正如問題所暗示的,當我將圖片加載到pictureBox(使用對話框)時,它不會以其原始外觀出現。在此屏幕拍攝中,左側的圖像是我加載到pictureBox(右側)中的圖像。圖片在PictureBox中旋轉
試圖知道是什麼導致這種情況我使用Paint應用繪製圖像,並使用Windows照片查看器旋轉它,旋轉後的圖像按原樣加載(旋轉),也就是說,一些圖片加載正常,其他圖片旋轉!我不明白爲什麼?!
正如問題所暗示的,當我將圖片加載到pictureBox(使用對話框)時,它不會以其原始外觀出現。在此屏幕拍攝中,左側的圖像是我加載到pictureBox(右側)中的圖像。圖片在PictureBox中旋轉
試圖知道是什麼導致這種情況我使用Paint應用繪製圖像,並使用Windows照片查看器旋轉它,旋轉後的圖像按原樣加載(旋轉),也就是說,一些圖片加載正常,其他圖片旋轉!我不明白爲什麼?!
沒有原始圖像數據,無法確定發生了什麼。但很明顯,在某些情況下,涉及圖像處理的某些軟件使用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方向(如果有的話)。
有了這個,你可以根據你的需要旋轉圖像。
當您在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); }
}
}
如果您使用「MyPictureBox」而不是「PictureBox」,則旋轉將自動修復。你也可以在圖像上手動使用'CorrectExifOrientation'方法,並將圖像放入'PictureBox'中。讓我知道如果你有任何問題的答案:) –
照片查看器通過調整EXIF數據旋轉JPG格式 - 它實際上並沒有改變像素數據。我猜這個圖片盒不會檢查JPG的EXIF方向。其他格式如BMP和PNG沒有EXIF,因此照片查看器實際上會在旋轉它們時修改像素數據。 – Blorgbeard