图像在PictureBox中旋转 [英] Images are rotated in PictureBox

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

问题描述

随着问题暗示,当我的图像加载到一个PictureBox(使用对话框),它不会显示为原来的样子。在此画面拍摄,左边的图像是我装到PictureBox一(右侧)。

As the question implies, when I load an image into a pictureBox (using dialog box), it doesn't appear as its original look. in this screen shoot, the image on the left is the one I loaded into the pictureBox (on the right).

试图了解是什么原因导致我用绘制图像涂料的应用程序和使用Windows照片查看器,加载它(旋转)旋转图像旋转它,就是它,有些照片就好了装,和其他旋转!我想不通,为什么?

Trying to know what causes this I draw an image using Paint app and rotated it using Windows Photo Viewer, the rotated image loaded as it is (rotated), that is it, some pictures are just fine loaded, and others are rotated! and I can't figure out why?!

推荐答案

如果没有原始图像数据,这是不可能的说肯定有什么事。但很显然,在某些时候,所涉及的图像的处理的一些软件采用了EXIF取向性旋转的图像,而不是实际修改的图像数据本身。这可以是照片浏览器或一些其它的工具,处理在某点的照片。

Without the original image data, it's impossible to say for sure what going on. But it's clear that at some point, some software involved in the processing of the image has used the EXIF orientation property to rotate the image, rather than actually modifying the image data itself. This may be Photo Viewer or some other tool that handled the photo at some point.

下面是代码可以使用以检测图像的方向,作为记录在相机EXIF数据拍的图片:

Here is code you can use to detect the orientation of the image, as recorded in the EXIF data by the camera that took the picture:

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



其中:

where:

/// <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( )上面的方法写为一个扩展方法,当然,你可以把它作为一个纯静态方法。无论哪种方式,只是通过它你刚刚从一个文件打开位图对象,它会返回存储在文件中的EXIF方向,如果有的话。

The GetOrientation() method above is written as an extension method, but of course you can call it as a plain static method. Either way, just pass it the Bitmap object you've just opened from a file, and it will return the EXIF orientation stored in the file, if any.

通过在手,你可以根据你的需要旋转图像。

With that in hand, you can rotate the image according to your needs.

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

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