在vb.net中更正图像方向服务器端 [英] Correcting Image Orientation server side in vb.net

查看:109
本文介绍了在vb.net中更正图像方向服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有人可以发布将执行此操作的代码吗?预先感谢.

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

推荐答案

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

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:

  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

这篇关于在vb.net中更正图像方向服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆