2

我使用 nuget for c# 下载了 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();
            }
        }
4

1 回答 1

4

如果您不保留 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();
    }
}
于 2016-09-26T04:55:41.503 回答