获得正确的图像旋转 [英] Getting correct Image rotation

查看:325
本文介绍了获得正确的图像旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题:当我将图像加载到Windows窗体 PictureBox 时,某些图片会被旋转,而其他图片则不会。

I have a simple problem: When I load an image to a windows form PictureBox some pictures are rotated and others are not.

基本上,用户选择带有 OpenFileDialog 的图片,并且当选择图片时:

Basically, a user selects a picture with an OpenFileDialog and when the picture is selected:

private void OpenFD_FileOk(object sender, CancelEventArgs e)
{
    Image image = Image.FromFile(openFD.FileName);
    PB_profile.Image = image;
}

是的,我检查了原始图像轮换

And yes I checked the original image rotation

编辑:

我将 PictureBox 属性 SizeMode 更改为 StretchImage

推荐答案

如果图片包含< a href =https://msdn.microsoft.com/en-us/library/ms534416.aspx?f=255&MSPPError=-2147217396 =nofollow noreferrer> exif data PropertyItems 应包含方向标记。

它对正确显示图像所需的旋转/翻转进行编码:

It encodes the rotation/flipping necessary to display the image correctly:


PropertyTagOrientation

PropertyTagOrientation

按行和列查看图像方向。

Image orientation viewed in terms of rows and columns.

标签0x0112

1 - 第0行位于
可视图像的顶部,第0列是视觉左侧。

2 - 第0个
行位于图像的可视顶部,第0列是
可视右侧。 br>
3 - 第0行位于
图像的可视底部,第0列是可视右侧。

4 - 第0行
在图像的视觉底部,第0列是视觉
左侧。

5 - 第0行是图像的视觉左侧,
第0列是视觉顶部。

6 - 第0行是图像的右侧
侧,第0列是视觉顶部。

7 - 第0行
行是图像的视觉右侧,第0列是
视觉底部。

8 - 第0行是图像的视觉左侧,
和第0列是视觉底部。

1 - The 0th row is at the top of the visual image, and the 0th column is the visual left side.
2 - The 0th row is at the visual top of the image, and the 0th column is the visual right side.
3 - The 0th row is at the visual bottom of the image, and the 0th column is the visual right side.
4 - The 0th row is at the visual bottom of the image, and the 0th column is the visual left side.
5 - The 0th row is the visual left side of the image, and the 0th column is the visual top.
6 - The 0th row is the visual right side of the image, and the 0th column is the visual top.
7 - The 0th row is the visual right side of the image, and the 0th column is the visual bottom.
8 - The 0th row is the visual left side of the image, and the 0th column is the visual bottom.

这是一个检索 PropertyItem的函数

PropertyItem getPropertyItemByID(Image img, int Id)
{
    return img.PropertyItems.Select(x => x).FirstOrDefault(x => x.Id == Id);
}

以下是使用GDI + RotateFlip <的示例/ code>动态调整图像的方法:

Here is an example of using the GDI+ RotateFlip method to adjust an image on the fly:

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);
}

它返回旋转版本..

我已用这套漂亮的样本图片

注意:代码仅在图像实际包含方向标记时才有效。如果他们不这样做,也许是因为他们是扫描,那么它将没有

Note: The code will only work if the images actually contain the orientation tag. If they don't, maybe because they are scans, then it will do nothing.

注意2 你写了我检查了原始图像旋转。这不是那么简单:资源管理器将显示已旋转的图像,所以在这里它们都看起来正确,甚至检查属性也不会显示方向!

Note 2 You wrote I checked the original image rotation. This is not so simple: The explorer will display the images already rotated, so here they all look right and even inspecting the properties doesn't reveal the orientation!

通常,当没有exif数据时, PropertyTagOrientation 标记 存在,但只有默认值 1 ..

Usually, when no exif data are present, the PropertyTagOrientation tag is present but only has the default value of 1..

更新:
如果图片没有拥有 PropertyTagOrientation 以下是如何添加一个:

Update: If the image doesn't have the PropertyTagOrientation here is how you can add one:

    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);

感谢@ ne1410s出色的在这里回答!

Kudos to @ne1410s's excellent answer here!.

注意添加图像的 PropertyItems 不会添加exif数据;这两个是不同的标签集!

Note that adding PropertyItems to an image does not add exif data; the two are different tag sets!

这篇关于获得正确的图像旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆