【问题标题】:.Net thumbnail is rotating image when being created from a mobile.Net 缩略图从移动设备创建时正在旋转图像
【发布时间】:2013-11-04 15:40:09
【问题描述】:

我正在对一个旧问题使用以下答案中的代码来设置上传图像的缩略图。

https://stackoverflow.com/a/2001462/1593395

这可以完美地填充图像,保持纵横比等,但如果我从手机上传图像,则通过此方法保存的缩略图图像会逆时针旋转 90 度。

你知道是什么原因造成的吗?原始图像只是使用 AjaxFileUpload1.SaveAs(MapPath("~/catalog/images/" & imageFilename)) 保存(来自 AJAX 控制工具包),并以正确的方向显示。

谢谢

【问题讨论】:

    标签: .net vb.net bitmap resize exif


    【解决方案1】:

    这可能是由于图像物理存储在不同的显示方向上,例如,使用相机侧面拍摄的 640*480 镜头可能会存储为 480*640,并带有方向 exif 数据标志。

    这很棒,因为 explorer/paint/photoshop/几乎每个查看者都会看到该 exif 标志并在渲染之前对其进行旋转。但是,.net Image 类不(当您知道发生了什么时这似乎是合理的),因此您必须在新的缩略图图像上设置 exif 旋转属性(我不喜欢这样做,只是因为我不喜欢缩略图上有任何属性)或自己检查和旋转缩略图。

    下面是一个粗略的方法。请注意,我已将 c# 中的代码作为您引用的答案的修改版本提供,因为那也是 c#。转换到 vb.net 应该非常简单:)

      if (sourceImage.PropertyIdList.Contains(0x112)) //0x112 = Orientation
      {
         var prop = sourceImage.GetPropertyItem(0x112);
         if (prop.Type == 3 && prop.Len == 2)
         {
            UInt16 orientationExif = BitConverter.ToUInt16(sourceImage.GetPropertyItem(0x112).Value, 0);
            if (orientationExif == 8)
            {
               newImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
            else if (orientationExif == 3)
            {
               newImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
            }
            else if (orientationExif == 6)
            {
               newImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
         }
      }
    

    所以更新后的 FixedSize 代码应该是这样的:

    static Image FixedSize(Image imgPhoto, int Width, int Height)
    {
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;
    
        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;
    
        nPercentW = ((float)Width / (float)sourceWidth);
        nPercentH = ((float)Height / (float)sourceHeight);
        if (nPercentH < nPercentW)
        {
            nPercent = nPercentH;
            destX = System.Convert.ToInt16((Width -
                          (sourceWidth * nPercent)) / 2);
        }
        else
        {
            nPercent = nPercentW;
            destY = System.Convert.ToInt16((Height -
                          (sourceHeight * nPercent)) / 2);
        }
    
        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);
    
        Bitmap bmPhoto = new Bitmap(Width, Height,
                          PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                         imgPhoto.VerticalResolution);
    
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.Red);
        grPhoto.InterpolationMode =
                InterpolationMode.HighQualityBicubic;
    
        grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);
    
        grPhoto.Dispose();
    
        //Rotate image to what is expected.
        if (imgPhoto.PropertyIdList.Contains(0x112)) //0x112 = Orientation
        {
           var prop = imgPhoto.GetPropertyItem(0x112);
           if (prop.Type == 3 && prop.Len == 2)
           {
              UInt16 orientationExif = BitConverter.ToUInt16(sourceImage.GetPropertyItem(0x112).Value, 0);
              if (orientationExif == 8)
              {
                 bmPhoto.RotateFlip(RotateFlipType.Rotate270FlipNone);
              }
              else if (orientationExif == 3)
              {
                 bmPhoto.RotateFlip(RotateFlipType.Rotate180FlipNone);
              }
              else if (orientationExif == 6)
              {
                 bmPhoto.RotateFlip(RotateFlipType.Rotate90FlipNone);
              }
           }
        }
    
        return bmPhoto;
    }
    

    请注意,这并不涵盖所有 exif 方向,仅涵盖常见的方向。

    参考资料:

    http://www.impulseadventure.com/photo/exif-orientation.html

    http://msdn.microsoft.com/en-us/library/xddt0dz7.aspx

    ps:这是我的第一个堆栈溢出答案,所以请在反馈中放轻松;)

    【讨论】:

    • 感谢您,我确实从另一个 Stackoverflow 问题中得到了类似的答案,该问题效果很好,但您的问题也一样好,因此接受了它。谢谢你帮我看
    • 仅供参考,要使用PropertyIdList.Contains(),您必须包含using System.Linq
    猜你喜欢
    • 2011-05-12
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多