自动旋转图片框中的图片

发布于 2024-10-03 20:40:32 字数 149 浏览 3 评论 0 原文

我的项目有一个用于加载图片的图片框控件,它工作正常。

然而,一些垂直的 jpg 图片在 Windows 资源管理器和我的图片框控件中水平显示 - 使用 Photoshop 等编辑器打开的同一文件显示垂直方向。

如何使图片在图片框控件中以正确的方向显示?

My project has a picturebox control for loading pictures, it's working fine.

However, some vertical jpg pictures are shown horizontally in Windows Explorer and also in my picturebox control - the same file opened using editors like Photoshop shows a vertical orientation.

How can I get the picture to show with the correct orientation in the picturebox control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-10-10 20:40:32

您需要检查图像并从 exif 标签中提取方向信息。

您需要做的第一件事就是获得一个 exif 阅读器。例如,代码项目上有一个用 VB.NET 编写的。

如果将文件加载到 Image 中,您将能够从 PropertyItems 读取 EXIF 属性(如此 C# 代码所示)

using (Image image = Image.FromFile(imageName))
{
    // Non property item properties
    this.FileName = imageName;
    PixelFormat = image.PixelFormat;
    this.Width = image.Size.Width;
    this.Height = image.Size.Height;

    foreach (PropertyItem pi in image.PropertyItems)
    {
        EXIFPropertyItem exifpi = new EXIFPropertyItem(pi);
        this.propertyItems.Add(exifpi);
    }
}

: > 是一个转换 PropertyItem 的类。 PropertyItemId 是 EXIF 代码(方向为 0x0112)。

然后查找 Orientation 属性并读取它的值。值 5、6、7 和 8 用于纵向(垂直)图像,例如,6 旋转 90 度,8 旋转 -90 度。

获得方向后,您可以调用适当的旋转变换以正确的方向显示图像。

You need to examine the image and extract the orientation information from the exif tags.

The first thing you'll need to do is get an exif reader. There's one written in VB.NET on Code Project for example.

If you load the file into an Image you will be able to read the EXIF properties from the PropertyItems (as this C# code demonstrates):

using (Image image = Image.FromFile(imageName))
{
    // Non property item properties
    this.FileName = imageName;
    PixelFormat = image.PixelFormat;
    this.Width = image.Size.Width;
    this.Height = image.Size.Height;

    foreach (PropertyItem pi in image.PropertyItems)
    {
        EXIFPropertyItem exifpi = new EXIFPropertyItem(pi);
        this.propertyItems.Add(exifpi);
    }
}

Where EXIFPropertyItem is a class that converts the PropertyItem. The PropertyItem's Id is the EXIF code (Orientation being 0x0112).

Then look for the the Orientation property and read it's value. Values 5, 6, 7 and 8 are for portrait (vertical) images, 6 being rotate 90 and 8 being rotate -90 for example.

Once you've got the orientation you can then call the appropriate rotation transformation to display the image in the correct orientation.

゛清羽墨安 2024-10-10 20:40:32

当您在图片框中显示图像时,它将以其原始方向显示。某些图像编辑应用程序能够检测图像的正确方向并自动旋转它们,但这将是一个相当难以实现的算法。

然而,手动旋转图片框中显示的图像几乎是微不足道的。只需使用 System.Drawing。 Image.RotateFlip 方法,由 .NET Framework 提供,指定 您希望其旋转的方向。例如,只需要一行代码:

myPictureBox.Image.RotateFlip(RotateFlipType.Rotate180FlipNone)

您也可以逐像素执行此操作,相对而言,这可能会更快,但如果您一次只旋转一张图像,我怀疑这是值得的。

When you display an image in a picture box, it will be displayed with its original orientation. Certain image editing applications are able to detect the proper orientation for your images and rotate them automatically, but that would be a fairly difficult algorithm to implement.

However, it's almost trivial to manually rotate an image displayed in a picture box. Just use the System.Drawing.Image.RotateFlip method that is provided by the .NET Framework, specifying the direction that you want it to be rotated. For example, only one line of code required:

myPictureBox.Image.RotateFlip(RotateFlipType.Rotate180FlipNone)

You could also do it pixel-by-pixel, which might turn out to be faster, relatively speaking, but I doubt it's worth it if you're only rotating one image at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文