当从一个移动正在创建的.Net缩略图旋转图像 [英] .Net thumbnail is rotating image when being created from a mobile

查看:357
本文介绍了当从一个移动正在创建的.Net缩略图旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是code从下面的答案对一个老问题,以建立一个上传图像的缩略图。

I am using the code from the following answer on an old question to set up a thumbnail of an uploaded image.

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

这完美的作品和衬垫的图像,保持宽高比等,但如果我上传的图像从我的手机是从这种方法保存旋转逆时针90度的缩略图。

This works perfectly and pads out the image, keeps aspect ratio etc but if I upload an image from my mobile phone the thumbnail image that is saved from this method is rotated ccw 90 degrees.

你知道是什么原因造成的?原始图像刚才保存使用AjaxFileUpload1.SaveAs(的MapPath(〜/目录/图片/&放大器;映像文件名称))。(从AJAX控件工具包),并显示在正确的方向。

Do you know what is causing this? The original image is just saved using AjaxFileUpload1.SaveAs(MapPath("~/catalog/images/" & imageFilename)) (From the AJAX Control toolkit) and is showing in the correct orientation.

感谢

推荐答案

也许这是由于图像是物理存储在不同的方向,而它的显示,例如,一个640 * 480的相机所拍摄当中采取可能存储为480 * 640,有定向EXIF数据标志。

Maybe it's due to the image being physically stored in a different orientation that which it's displayed in, eg, a 640*480 shot taken with the camera sideways may be stored as 480*640, with an orientation exif data flag.

这是伟大的,因为浏览器/油漆/软件/几乎每一位观众将看到EXIF国旗和旋转它渲染之前。然而,.NET图像类不(这似乎是合理的,当你知道发生了什么),所以你必须要么设置EXIF旋转ATTRIB新的缩略图上(我preFER不是,只是因为我不T就像任何attribs在缩略图)或检查和旋转缩略图自己。

This is great because explorer/paint/photoshop/just about every viewer will see that exif flag and rotate it before it is rendered. However, .net Image classes dont (which seems reasonable when you know what is happening), so you'll have to either set the exif rotate attrib on the new thumbnail image (which I prefer not to, just because I don't like having any attribs on thumbnails) or check and rotate the thumbnail yourself.

下面是一个粗略的办法做到这一点。请注意,我已经提供了code在C#中,你所引用的回答修改后的版本,因为这也是C#。转换为vb.net应该是pretty的直线前进:)

Below is a rough way to do this. Note that I've supplied the code in c# as a modified version of the answer you referenced, as that was also c#. Conversion to vb.net should be pretty straight forward :)

  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 code会是这样:

So the updated FixedSize code would be as such:

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方向,只是常见的。

Note that this doesn't cover EVERY exif orientation, just common ones.

参考文献:

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

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

PS:这是我的第一个堆栈溢出的答案,所以请去容易的反馈;)

p.s: that was my first stack overflow answer, so please go easy on the feedback ;)

这篇关于当从一个移动正在创建的.Net缩略图旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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