ImageProcessor似乎在调整大小后旋转图像90度

本文关键字:旋转 图像 90度 调整 ImageProcessor | 更新日期: 2023-09-27 18:15:14

我使用c#的nuget下载了ImageProcessor库。我用它来上传和调整一个网站的图像大小。上传过程工作得很好,除了当我试图查看上传的图像时,它看起来比原始图像向后旋转90度。下面是我使用的代码:

        ISupportedImageFormat format = new JpegFormat { Quality = 70 };
        using (MemoryStream inStream = new MemoryStream(_img))
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                {
                    // Load, resize, set the format and quality and save an image.
                    imageFactory.Load(inStream)
                        .Resize(new ResizeLayer(new Size(width, height), resizeMode: resizeMode))
                                .Format(format)
                                .Save(outStream);
                }
                return outStream.ToArray();
            }
        }

ImageProcessor似乎在调整大小后旋转图像90度

如果您不保留EXIF元数据,ImageFactory类有一个方法AutoRotate,它将改变图像以补偿原始方向。

http://imageprocessor.org/imageprocessor/imagefactory/autorotate/

您的新代码将如下所示:

ISupportedImageFormat format = new JpegFormat { Quality = 70 };
using (MemoryStream inStream = new MemoryStream(_img))
{
    using (MemoryStream outStream = new MemoryStream())
    {
        // Initialize the ImageFactory using the overload to preserve EXIF metadata.
        using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
        {
            // Load, resize, set the format and quality and save an image.
            imageFactory.Load(inStream)
                        .AutoRotate()
                        .Resize(new ResizeLayer(new Size(width, height), resizeMode: resizeMode))
                        .Format(format)
                        .Save(outStream);
        }
        return outStream.ToArray();
    }
}