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

Obi*_*Wan 3 vb.net exif image orientation

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

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

Obi*_*Wan 15

对于任何需要这个的人,我基本上在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
Run Code Online (Sandbox Code Playgroud)