自动旋转图片框中的图片
我的项目有一个用于加载图片的图片框控件,它工作正常。
然而,一些垂直的 jpg 图片在 Windows 资源管理器和我的图片框控件中水平显示 - 使用 Photoshop 等编辑器打开的同一文件显示垂直方向。
如何使图片在图片框控件中以正确的方向显示?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要检查图像并从 exif 标签中提取方向信息。
您需要做的第一件事就是获得一个 exif 阅读器。例如,代码项目上有一个用 VB.NET 编写的。
如果将文件加载到
Image
中,您将能够从PropertyItems
读取 EXIF 属性(如此 C# 代码所示): > 是一个转换
PropertyItem
的类。PropertyItem
的Id
是 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 thePropertyItems
(as this C# code demonstrates):Where
EXIFPropertyItem
is a class that converts thePropertyItem
. ThePropertyItem
'sId
is the EXIF code (Orientation being0x0112
).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.
当您在图片框中显示图像时,它将以其原始方向显示。某些图像编辑应用程序能够检测图像的正确方向并自动旋转它们,但这将是一个相当难以实现的算法。
然而,手动旋转图片框中显示的图像几乎是微不足道的。只需使用
System.Drawing。 Image.RotateFlip
方法,由 .NET Framework 提供,指定 您希望其旋转的方向。例如,只需要一行代码:您也可以逐像素执行此操作,相对而言,这可能会更快,但如果您一次只旋转一张图像,我怀疑这是值得的。
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: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.