使用 GDI 旋转图像

发布于 2024-12-11 20:38:27 字数 175 浏览 0 评论 0原文

我正在使用 GDI+ 进行图像旋转。根据图像元数据,应该有 8 种不同类型的方向 (http://www.impulseadventure.com/photo/exif-orientation.html)。但我得到的所有图像都具有相同的方向,无论它们是水平方向还是垂直方向。谁能建议问题出在哪里或者我错过了什么?

谢谢!

I am working on image rotation using GDI+. As per image metadata, there should be 8 different types of orientation (http://www.impulseadventure.com/photo/exif-orientation.html). But I am getting the same orientation for all the images whether they are in Horizontal or Vertical orientation. Can anyone suggest where is the problem or am I missing something ?

Thanks!

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

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

发布评论

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

评论(2

白况 2024-12-18 20:38:27

几天前,我将自动旋转添加到 imageresizing.net 库 作为 AutoRotate 插件。我提供了相关的源代码,希望对您有所帮助。

if (!"true".Equals(settings["autorotate"], StringComparison.OrdinalIgnoreCase)) return RequestedAction.None;

int propertyId = 0x0112;
PropertyItem pi;
try {
    pi = b.GetPropertyItem(propertyId);
} catch (ArgumentException) {
    return RequestedAction.None;
}
if (pi == null) return RequestedAction.None;

int total = 0;

foreach (byte by in pi.Value) total += by; //Does not handle values larger than 255, but it doesn't need to, and is endian-agnostic.

if (total == 8) b.RotateFlip(RotateFlipType.Rotate270FlipNone);
if (total == 3) b.RotateFlip(RotateFlipType.Rotate180FlipNone);
if (total == 6) b.RotateFlip(RotateFlipType.Rotate90FlipNone);

if (total == 2) b.RotateFlip(RotateFlipType.RotateNoneFlipX);
if (total == 4) b.RotateFlip(RotateFlipType.Rotate180FlipX);
if (total == 5) b.RotateFlip(RotateFlipType.Rotate270FlipY);
if (total == 7) b.RotateFlip(RotateFlipType.Rotate90FlipY);

b.RemovePropertyItem(propertyId);

仅供参考,如果您在 ASP.NET 中调整图像大小,您应该阅读这篇文章,了解如何安全地执行此操作,或者使用 ImageResizing.Net 库

A couple days ago I added automatic rotation to the imageresizing.net library as the AutoRotate plugin. I'm including the relevant source code, which should hopefully help you.

if (!"true".Equals(settings["autorotate"], StringComparison.OrdinalIgnoreCase)) return RequestedAction.None;

int propertyId = 0x0112;
PropertyItem pi;
try {
    pi = b.GetPropertyItem(propertyId);
} catch (ArgumentException) {
    return RequestedAction.None;
}
if (pi == null) return RequestedAction.None;

int total = 0;

foreach (byte by in pi.Value) total += by; //Does not handle values larger than 255, but it doesn't need to, and is endian-agnostic.

if (total == 8) b.RotateFlip(RotateFlipType.Rotate270FlipNone);
if (total == 3) b.RotateFlip(RotateFlipType.Rotate180FlipNone);
if (total == 6) b.RotateFlip(RotateFlipType.Rotate90FlipNone);

if (total == 2) b.RotateFlip(RotateFlipType.RotateNoneFlipX);
if (total == 4) b.RotateFlip(RotateFlipType.Rotate180FlipX);
if (total == 5) b.RotateFlip(RotateFlipType.Rotate270FlipY);
if (total == 7) b.RotateFlip(RotateFlipType.Rotate90FlipY);

b.RemovePropertyItem(propertyId);

As an FYI, if you are doing image resizing in ASP.NET, you should read this article on how to do it safely, or use the ImageResizing.Net library instead.

在你怀里撒娇 2024-12-18 20:38:27

只是一个小改进,

我不会测试参数异常,这会消耗 CPU 周期。

   var orientation_index = Array.IndexOf(b.PropertyIdList, propertyId );

        if ( orientation_index <0) return RequestedAction.None;
        byte total =0;
        foreach (byte b in b.GetPropertyItem(OrientationId).Value)
        {
            total += b;
        }

Just a small improvement,

I would not test for an argumentexception, that costs CPU cycles.

   var orientation_index = Array.IndexOf(b.PropertyIdList, propertyId );

        if ( orientation_index <0) return RequestedAction.None;
        byte total =0;
        foreach (byte b in b.GetPropertyItem(OrientationId).Value)
        {
            total += b;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文