代码之家  ›  专栏  ›  技术社区  ›  Diego Jancic

.NET exif信息不在生产服务器上工作

  •  0
  • Diego Jancic  · 技术社区  · 6 年前

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

    有什么建议吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Diego Jancic    6 年前

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