.NET Exif信息在生产服务器上不起作用 [英] .NET Exif information not working on production server
问题描述
我正在读取JPEG的Exif信息以旋转图像. JPEG在ASP.NET中上传,我读取了上传流,对其进行旋转并保存.它可以在我的开发机(Windows 10,IIS 10)上正常运行,但是当我在服务器(Windows Server 2012 R2,IIS 8.5)上尝试时,它无法正常工作,并且不会加载任何Exif信息.
I'm reading the Exif information of a JPEG to rotate an image. The JPEG gets uploaded in ASP.NET and I read the upload stream, rotate it and save it. It's working perfectly on my dev machine (Windows 10, IIS 10), but when I try on the server (Windows Server 2012 R2, IIS 8.5), it doesn't work, it doesn't load any Exif information.
代码如下:
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
- Works: Windows 10, IIS 10
- Doesn't work: Windows Server 2012 R2, IIS 8.5
有什么建议吗?
推荐答案
以防万一有人遇到相同的问题.我在生产中使用WFP和GDI读取方向时遇到问题.
Just in case someone has the same issue. I had problems in production reading the orientation using WFP and GDI.
使用WPF时,错误为:
When using WPF, the error was:
System.Runtime.InteropServices.COMException (0x88982F8A): The component registration is invalid.
(Exception from HRESULT: 0x88982F8A)
at System.Windows.Media.Imaging.BitmapMetadata.GetQuery(String query)
解决方案:
唯一有效的方法是使用: https://github.com/dlemstra/Magick.NET
The only thing that worked was using: https://github.com/dlemstra/Magick.NET
代码非常简单:
var img = new MagickImage(inputStream);
img.AutoOrient(); // Fix orientation
img.Strip(); // remove all EXIF information
img.Write(outputPath);
它还帮助我删除了10条线.
It also helped me remove 10s of lines.
这篇关于.NET Exif信息在生产服务器上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!