2016-08-21 118 views
2

正如问题所暗示的,当我将图片加载到pictureBox(使用对话框)时,它不会以其原始外观出现。在此屏幕拍摄中,左侧的图像是我加载到pictureBox(右侧)中的图像。图片在PictureBox中旋转

试图知道是什么导致这种情况我使用Paint应用绘制图像,并使用Windows照片查看器旋转它,旋转后的图像按原样加载(旋转),也就是说,一些图片加载正常,其他图片旋转!我不明白为什么?! enter image description here

+1

照片查看器通过调整EXIF数据旋转JPG格式 - 它实际上并没有改变像素数据。我猜这个图片盒不会检查JPG的EXIF方向。其他格式如BMP和PNG没有EXIF,因此照片查看器实际上会在旋转它们时修改像素数据。 – Blorgbeard

回答

4

没有原始图像数据,无法确定发生了什么。但很明显,在某些情况下,涉及图像处理的某些软件使用EXIF方向属性来旋转图像,而不是实际修改图像数据本身。这可能是照片查看器或某些其他处理照片的工具。

这里是代码,你可以用它来检测图像的方向,记录在相机的EXIF数据拍的图片:

static ImageOrientation GetOrientation(this Image image) 
{ 
    PropertyItem pi = SafeGetPropertyItem(image, 0x112); 

    if (pi == null || pi.Type != 3) 
    { 
     return ImageOrientation.Original; 
    } 

    return (ImageOrientation)BitConverter.ToInt16(pi.Value, 0); 
} 

// A file without the desired EXIF property record will throw ArgumentException. 
static PropertyItem SafeGetPropertyItem(Image image, int propid) 
{ 
    try 
    { 
     return image.GetPropertyItem(propid); 
    } 
    catch (ArgumentException) 
    { 
     return null; 
    } 
} 

其中:

/// <summary> 
/// Possible EXIF orientation values describing clockwise 
/// rotation of the captured image due to camera orientation. 
/// </summary> 
/// <remarks>Reverse/undo these transformations to display an image correctly</remarks> 
public enum ImageOrientation 
{ 
    /// <summary> 
    /// Image is correctly oriented 
    /// </summary> 
    Original = 1, 
    /// <summary> 
    /// Image has been mirrored horizontally 
    /// </summary> 
    MirrorOriginal = 2, 
    /// <summary> 
    /// Image has been rotated 180 degrees 
    /// </summary> 
    Half = 3, 
    /// <summary> 
    /// Image has been mirrored horizontally and rotated 180 degrees 
    /// </summary> 
    MirrorHalf = 4, 
    /// <summary> 
    /// Image has been mirrored horizontally and rotated 270 degrees clockwise 
    /// </summary> 
    MirrorThreeQuarter = 5, 
    /// <summary> 
    /// Image has been rotated 270 degrees clockwise 
    /// </summary> 
    ThreeQuarter = 6, 
    /// <summary> 
    /// Image has been mirrored horizontally and rotated 90 degrees clockwise. 
    /// </summary> 
    MirrorOneQuarter = 7, 
    /// <summary> 
    /// Image has been rotated 90 degrees clockwise. 
    /// </summary> 
    OneQuarter = 8 
} 

GetOrientation()上面的方法是作为扩展方法编写的,但当然,您可以将其称为纯静态方法。无论采用哪种方式,只需将它刚刚从文件打开的Bitmap对象传递给它,它将返回存储在文件中的EXIF方向(如果有的话)。

有了这个,你可以根据你的需要旋转图像。

1

当您在Windows照片查看器中查看图像时,如果它具有Exif方向数据,它将自动更正图像方向。 PictureBox没有内置的对这种功能的支持。您可以创建自定义PictureBox其正确,即使他们有方向的数据显示图片:

using System.Linq; 
using System.Windows.Forms; 
using System.Drawing; 
using System.ComponentModel; 
public class MyPictureBox : PictureBox 
{ 
    private void CorrectExifOrientation(Image image) 
    { 
     if (image == null) return; 
     int orientationId = 0x0112; 
     if (image.PropertyIdList.Contains(orientationId)) 
     { 
      var orientation = (int)image.GetPropertyItem(orientationId).Value[0]; 
      var rotateFlip = RotateFlipType.RotateNoneFlipNone; 
      switch (orientation) 
      { 
       case 1: rotateFlip = RotateFlipType.RotateNoneFlipNone; break; 
       case 2: rotateFlip = RotateFlipType.RotateNoneFlipX; break; 
       case 3: rotateFlip = RotateFlipType.Rotate180FlipNone; break; 
       case 4: rotateFlip = RotateFlipType.Rotate180FlipX; break; 
       case 5: rotateFlip = RotateFlipType.Rotate90FlipX; break; 
       case 6: rotateFlip = RotateFlipType.Rotate90FlipNone; break; 
       case 7: rotateFlip = RotateFlipType.Rotate270FlipX; break; 
       case 8: rotateFlip = RotateFlipType.Rotate270FlipNone; break; 
       default: rotateFlip = RotateFlipType.RotateNoneFlipNone; break; 
      } 
      if (rotateFlip != RotateFlipType.RotateNoneFlipNone) 
      { 
       image.RotateFlip(rotateFlip); 
       image.RemovePropertyItem(orientationId); 
      } 
     } 
    } 
    [Localizable(true)] 
    [Bindable(true)] 
    public new Image Image 
    { 
     get { return base.Image; } 
     set { base.Image = value; CorrectExifOrientation(value); } 
    } 
} 
+0

如果您使用“MyPictureBox”而不是“PictureBox”,则旋转将自动修复。你也可以在图像上手动使用'CorrectExifOrientation'方法,并将图像放入'PictureBox'中。让我知道如果你有任何问题的答案:) –