我有一个简单的问题:当我将图像加载到Windows窗体时,PictureBox一些图片会旋转,而其他图片则不会.
基本上,用户选择带有OpenFileDialog和当选择图片时的图片:
private void OpenFD_FileOk(object sender, CancelEventArgs e)
{
Image image = Image.FromFile(openFD.FileName);
PB_profile.Image = image;
}
Run Code Online (Sandbox Code Playgroud)
是的,我检查了原始图像旋转
编辑:
我改变了PictureBox性质SizeMode,以StretchImage
如果图片包含exif数据,PropertyItems则应包含方向标记.
它编码正确显示图像所需的旋转/翻转:
PropertyTagOrientation
按行和列查看图像方向.
标签0x0112
1 - 第0行位于可视图像的顶部,第0列是可视左侧.
2 - 第0行位于图像的可视顶部,第0列是视觉右侧.
3 - 第0行位于图像的可视底部,第0列是可视右侧.
4 - 第0行位于图像的可视底部,第0列是可视左侧.
5 - 第0行是图像的可视左侧,第0列是可视顶部.
6 - 第0行是图像的视觉右侧,第0列是视觉顶部.
7 - 第0行是图像的视觉右侧,第0列是视觉底部.
8 - 第0行是图像的视觉左侧,第0列是视觉底部.
这是一个检索以下内容的函数PropertyItem:
PropertyItem getPropertyItemByID(Image img, int Id)
{
return img.PropertyItems.Select(x => x).FirstOrDefault(x => x.Id == Id);
}
Run Code Online (Sandbox Code Playgroud)
以下是使用GDI + RotateFlip方法动态调整图像的示例:
void Rotate(Bitmap bmp)
{
PropertyItem pi = bmp.PropertyItems.Select(x => x)
.FirstOrDefault(x => x.Id == 0x0112);
if (pi == null) return;
byte o = pi.Value[0];
if (o==2) bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
if (o==3) bmp.RotateFlip(RotateFlipType.RotateNoneFlipXY);
if (o==4) bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
if (o==5) bmp.RotateFlip(RotateFlipType.Rotate90FlipX);
if (o==6) bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
if (o==7) bmp.RotateFlip(RotateFlipType.Rotate90FlipY);
if (o==8) bmp.RotateFlip(RotateFlipType.Rotate90FlipXY);
}
Run Code Online (Sandbox Code Playgroud)
它返回旋转版本..
我已经使用这套漂亮的样本图像测试了值.
注意:只有图像实际包含方向标记时,代码才有效.如果他们不这样做,也许是因为他们是扫描,那么它什么都不做.
注意2您写道我检查了原始图像旋转.这不是那么简单:资源管理器将显示已旋转的图像,所以在这里它们都看起来正确,甚至检查属性也不会显示方向!
通常情况下,当没有EXIF数据存在时,PropertyTagOrientation标签是存在的,但仅有的默认值1..
更新:
如果图像不具备PropertyTagOrientation这里是你如何可以添加一个:
using System.Runtime.Serialization;
..
pi = (PropertyItem)FormatterServices
.GetUninitializedObject(typeof(PropertyItem));
pi.Id = 0x0112; // orientation
pi.Len = 2;
pi.Type = 3;
pi.Value = new byte[2] { 1, 0 };
pi.Value[0] = yourOrientationByte;
yourImage.SetPropertyItem(pi);
Run Code Online (Sandbox Code Playgroud)
请注意,添加PropertyItems到图像不会添加exif数据; 这两个是不同的标签集!
| 归档时间: |
|
| 查看次数: |
2137 次 |
| 最近记录: |