我正在读取jpeg的exif信息以旋转图像。jpeg在ASP.NET中上载,我读取上载流,旋转并保存它。它在我的开发人员计算机(Windows 10、IIS 10)上运行良好,但是当我在服务器(Windows Server 2012 R2、IIS 8.5)上尝试时,它不工作,它不加载任何exif信息。
代码如下:
void SavePhoto()
{
// PHOTO is the Html
HttpPostedFile photo = Request.Files["ProfilePhoto_File"];
using (var image = Image.FromStream(photo.InputStream, true, true))
{
SaveConvertingFormat(image, "output_path.jpg");
}
}
public static void SaveConvertingFormat(Image image, string outputPath)
{
int imageWidth = image.Width;
int imageHeight = image.Height;
using (var result = new Bitmap(imageWidth, imageHeight))
{
using (var g = Graphics.FromImage(result))
{
g.DrawImage(image, 0, 0, imageWidth, imageHeight);
}
var rotation = GetExifRotate(image, outputPath);
// IN THE SERVER, rotation IS ALWAYS RotateNoneFlipNone
if (rotation != RotateFlipType.RotateNoneFlipNone)
result.RotateFlip(rotation);
SaveJpeg(result, outputPath, 85);
}
}
private static void SaveJpeg(this Image img, string filename, int quality)
{
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (long)quality);
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(filename, jpegCodec, encoderParams);
}
public static RotateFlipType GetExifRotate(Image img, string outputPath)
{
// Source: https://stackoverflow.com/a/48347653/72350
// ERROR:
// IN THE PRODUCTION SERVER, PropertyIdList IS EMPTY!
const int ExifOrientationId = 0x112;
if (!img.PropertyIdList.Contains(ExifOrientationId))
return RotateFlipType.RotateNoneFlipNone;
var prop = img.GetPropertyItem(ExifOrientationId);
int val = BitConverter.ToUInt16(prop.Value, 0);
var rot = RotateFlipType.RotateNoneFlipNone;
if (val == 3 || val == 4)
rot = RotateFlipType.Rotate180FlipNone;
else if (val == 5 || val == 6)
rot = RotateFlipType.Rotate90FlipNone;
else if (val == 7 || val == 8)
rot = RotateFlipType.Rotate270FlipNone;
if (val == 2 || val == 4 || val == 5 || val == 7)
rot |= RotateFlipType.RotateNoneFlipX;
return rot;
}
同样,上面的代码:
-
作品
:Windows 10,IIS 10
-
不起作用
:Windows Server 2012 R2,IIS 8.5
有什么建议吗?