Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.Net缩略图是从移动设备创建时旋转的图像_.net_Vb.net_Bitmap_Resize_Exif - Fatal编程技术网

.Net缩略图是从移动设备创建时旋转的图像

.Net缩略图是从移动设备创建时旋转的图像,.net,vb.net,bitmap,resize,exif,.net,Vb.net,Bitmap,Resize,Exif,我正在使用下面关于一个老问题的答案中的代码来设置上传图像的缩略图 这种方法非常有效,可以填充图像,保持纵横比等,但如果我从手机上传图像,则通过这种方法保存的缩略图会逆时针旋转90度 你知道这是什么原因吗?原始图像仅使用AjaxFileUpload1.SaveAsMapPath~/catalog/images/&imageFilename从AJAX控制工具包中保存,并以正确的方向显示 感谢可能是因为图像物理存储的方向与显示的方向不同,例如,使用相机侧向拍摄的640*480照片可能存储为480*64

我正在使用下面关于一个老问题的答案中的代码来设置上传图像的缩略图

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

你知道这是什么原因吗?原始图像仅使用AjaxFileUpload1.SaveAsMapPath~/catalog/images/&imageFilename从AJAX控制工具包中保存,并以正确的方向显示


感谢

可能是因为图像物理存储的方向与显示的方向不同,例如,使用相机侧向拍摄的640*480照片可能存储为480*640,并带有方向exif数据标志

这很好,因为explorer/paint/photoshop/几乎每个查看器都会看到exif标志,并在渲染前旋转它。但是,.net图像类没有,这在您知道发生了什么时似乎是合理的,因此您必须在新的缩略图图像上设置exif rotate attrib,我不喜欢这样,因为我不喜欢在缩略图上设置任何attrib,或者自己检查并旋转缩略图

下面是一个粗略的方法。请注意,我用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方向,而是普通方向

参考资料:


p、 s:这是我第一次回答堆栈溢出问题,所以请不要太在意反馈

谢谢你的回答,我确实从另一个Stackoverflow问题中得到了类似的答案,这个问题很有效,但你的问题也一样好,所以我接受了。感谢您为meFYI查看它,以使用PropertyIdList。包含您必须包括使用System.Linq。