在 vb.net 中更正图像方向服务器端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19306564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:27:24  来源:igfitidea点击:

Correcting Image Orientation server side in vb.net

vb.netimageorientationexif

提问by Obi Wan

In a mobile web application I am developing, users are allowed to take a picture with their camera and the camera image is uploaded to a server. The issue I am having is that on iOS devices, images get an EXIF Orientation tag associated with them such as "ROTATE 90 CW". This orientation tag causes the image to be displayed in an incorrect orientation when it is displayed. For example, if the user takes a picture of something with their iPhone in portrait orientation, the image appears to be rotated to landscape when viewed on the server. I want to correct this issue on the server-side using VB.Net so that I automatically detect the EXIF Orientation tag and if it is "ROTATE 90 CW" (or any other value that will make the image appear to be displayed incorrectly), then I want to automatically rotate the image to the correct orientation. In summary, I want the image on the server to appear exactly as it appeared when the user took the picture with their camera.

在我正在开发的移动 Web 应用程序中,允许用户使用他们的相机拍照并将相机图像上传到服务器。我遇到的问题是,在 iOS 设备上,图像会获得一个与其关联的 EXIF 方向标签,例如“ROTATE 90 CW”。此方向标记会导致图像在显示时以错误的方向显示。例如,如果用户使用 iPhone 纵向拍摄某物的照片,则在服务器上查看时,图像似乎已旋转为横向。我想使用 VB.Net 在服务器端更正这个问题,以便我自动检测 EXIF 方向标签,如果它是“ROTATE 90 CW”(或任何其他会使图像显示不正确的值),然后我想自动将图像旋转到正确的方向。总而言之,我希望服务器上的图像与用户用相机拍摄照片时的图像完全一样。

Can someone post the code that will do this? Thanks in advance.

有人可以发布可以执行此操作的代码吗?提前致谢。

回答by Obi Wan

For anyone who needs this, I basically resolved the issue using this code in VB.Net. I found this to be just what I needed:

对于需要此功能的任何人,我基本上在 VB.Net 中使用此代码解决了该问题。我发现这正是我所需要的:

  Public Function TestRotate(sImageFilePath As String) As Boolean
    Dim rft As RotateFlipType = RotateFlipType.RotateNoneFlipNone
    Dim img As Bitmap = Image.FromFile(sImageFilePath)
    Dim properties As PropertyItem() = img.PropertyItems
    Dim bReturn As Boolean = False
    For Each p As PropertyItem In properties
      If p.Id = 274 Then
        Dim orientation As Short = BitConverter.ToInt16(p.Value, 0)
        Select Case orientation
          Case 1
            rft = RotateFlipType.RotateNoneFlipNone
          Case 3
            rft = RotateFlipType.Rotate180FlipNone
          Case 6
           rft = RotateFlipType.Rotate90FlipNone
          Case 8
           rft = RotateFlipType.Rotate270FlipNone
        End Select
      End If
    Next
    If rft <> RotateFlipType.RotateNoneFlipNone Then
      img.RotateFlip(rft)
      System.IO.File.Delete(sImageFilePath)
      img.Save(sImageFilePath, System.Drawing.Imaging.ImageFormat.Jpeg)
      bReturn = True
    End If
    Return bReturn

  End Function

回答by aswallows

For anyone interested... C# version.

对于任何感兴趣的人... C# 版本。

public static bool TestRotate(string filePath)
{
    var rft = RotateFlipType.RotateNoneFlipNone;
    var img = Image.FromFile(filePath);
    var properties = img.PropertyItems;
    var value = false;

    foreach (var prop in properties.Where(i => i.Id == 274))
    {
        var orientation = BitConverter.ToInt16(prop.Value, 0);
        rft = orientation == 1 ? RotateFlipType.RotateNoneFlipNone :
                orientation == 3 ? RotateFlipType.Rotate180FlipNone :
                orientation == 6 ? RotateFlipType.Rotate90FlipNone :
                orientation == 8 ? RotateFlipType.Rotate270FlipNone :
                RotateFlipType.RotateNoneFlipNone;
    }

    if (rft != RotateFlipType.RotateNoneFlipNone)
    {
        img.RotateFlip(rft);
        File.Delete(filePath);
        img.Save(filePath, ImageFormat.Jpeg);
        value = true;
    }

    return value;
}