.NET Exif信息无法在生产服务器上运行

问题描述 投票:0回答:2

我正在阅读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;
}

再次,上面的代码:

  • Works:Windows 10,IIS 10
  • 不起作用:Windows Server 2012 R2,IIS 8.5

有什么建议?

c# asp.net image-processing exif
2个回答
1
投票

无需剥离EXIF数据。 AutoOrient()自动将EXIF方向设置为TopLeft。

您还需要使用using子句,因为MagickImage实现了IDisposable。

using (var img = new MagickImage(inputStream))
{
    img.AutoOrient();   // Fix orientation
    img.Write(outputPath);
}

0
投票

以防有人有同样的问题。我在生产中使用WFP和GDI阅读方向时遇到了问题。

使用WPF时,错误是:

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

代码很简单:

var img = new MagickImage(inputStream);
img.AutoOrient();   // Fix orientation
img.Strip();        // remove all EXIF information
img.Write(outputPath);

它还帮我删除了10行。

© www.soinside.com 2019 - 2024. All rights reserved.