【问题标题】:.NET Exif information not working on production server.NET Exif 信息在生产服务器上不起作用
【发布时间】:2019-05-01 22:26:24
【问题描述】:

我正在读取 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

有什么建议吗?

【问题讨论】:

    标签: c# asp.net image-processing exif


    【解决方案1】:

    以防万一有人遇到同样的问题。我在使用 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 行。

    【讨论】:

      【解决方案2】:

      没有必要去除 EXIF 数据。 AutoOrient() 自动将 EXIF 方向设置为 TopLeft。

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

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

      【讨论】:

      • 这与接受的答案相同。还是谢谢你。
      猜你喜欢
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 2020-05-20
      • 2014-06-15
      • 1970-01-01
      • 2011-08-04
      相关资源
      最近更新 更多