如何使用ImageSharp(Web)压缩/更改图像流(IFormFile)

时间:2019-08-11 11:12:22

标签: c# asp.net-core .net-core imagesharp

我正在尝试使用ImageSharp.Web()库压缩图像的质量/大小(通常为5-30左右),我无法真正理解该怎么做或我在这里缺少什么。

  • 我可以重用相同的内存流/ IFormFile对象来保存变异的图像吗?还是我需要根据当前图像对象创建一个新图像?
  • 要使用内存流,我还需要使用特定的JpegDecoder()吗?
  • 不确定此行是否正确 item.SaveAsJpeg(memoryStream);
  • 也许有人可以在逻辑上帮助我,否则任何提示或技巧将非常有帮助。谢谢!

简单代码示例:

private byte[] ConvertImageToByteArray(IFormFile inputImage)
{
    byte[] result = null;

    // filestream
    using (var fileStream = inputImage.OpenReadStream()) // IFormFile inputImage

    // memory stream
    using (var memoryStream = new MemoryStream())
    {
        fileStream.CopyTo(memoryStream);
        memoryStream.Position = 0; // The position needs to be reset.

        var before = memoryStream.Length;

        using (var item = Image.Load(memoryStream)) // do I need to use here JpegDecoder?
        {
            var beforeMutations = item.Size();

            // dummy resize options
            int width = 50;
            int height = 100;
            IResampler sampler = KnownResamplers.Lanczos3;
            bool compand = true;
            ResizeMode mode = ResizeMode.Stretch;

            // init resize object
            var resizeOptions = new ResizeOptions
            {
                Size = new Size(width, height),
                Sampler = sampler,
                Compand = compand,
                Mode = mode
            };

            // mutate image
            item.Mutate(x => x
                 .Resize(resizeOptions)
                 .Rotate(35));

            var afterMutations = item.Size();

            // try to save mutated image back to memory stream / overwrite
            // this is not overwriting memory stream
            item.SaveAsJpeg(memoryStream);

            // prepare result to byte[]
            result = memoryStream.ToArray();
        }

        var after = fileStream.Length; // kind of not needed.
        }

1 个答案:

答案 0 :(得分:4)

我知道这篇文章有些过时了,我相信您已经解决了您的问题,但是希望它将对以后的工作有所帮助。

我可以重用相同的内存流/ IFormFile对象来保存变异图像吗?还是我需要根据当前图像对象创建一个新图像?

这里有两个流,您可以对IFormFile中的图像使用一个流,并且可以在此处操作所有内容,然后使用MemoryStream保存到

要使用内存流,我还需要使用特定的JpegDecoder()吗?

您可以使用.SaveAsJpeg(memoryStream)将图像保存到内存流中

不确定此行是否正确。SaveAsJpeg(memoryStream);。

您在正确的轨道上

也许有人可以在逻辑上帮助我,否则任何提示或技巧将非常有帮助。谢谢!

这是我根据您所拥有的内容进行重写的内容:希望对您有所帮助。我敢肯定,有些事情可以简化,我试图使其保持接近OPs格式

    private byte[] ConvertImageToByteArray(IFormFile inputImage)
    {
        byte[] result = null;

        // memory stream
        using (var memoryStream = new MemoryStream())
        // filestream
        using (var image = Image.Load(inputImage.OpenReadStream())) // IFormFile inputImage
        {
            //var before = memoryStream.Length; Removed this, assuming you are using for debugging?
            var beforeMutations = image.Size();

            // dummy resize options
            int width = 50;
            int height = 100;
            IResampler sampler = KnownResamplers.Lanczos3;
            bool compand = true;
            ResizeMode mode = ResizeMode.Stretch;

            // init resize object
            var resizeOptions = new ResizeOptions
            {
                Size = new Size(width, height),
                Sampler = sampler,
                Compand = compand,
                Mode = mode
            };

            // mutate image
            image.Mutate(x => x
                 .Resize(resizeOptions)
                 .Rotate(35));

            var afterMutations = image.Size();

            //Encode here for quality
            var encoder = new JpegEncoder()
            {
                Quality = 30 //Use variable to set between 5-30 based on your requirements
            };

            //This saves to the memoryStream with encoder
            image.Save(memoryStream, encoder);
            memoryStream.Position = 0; // The position needs to be reset.

            // prepare result to byte[]
            result = memoryStream.ToArray();

            var after = memoryStream.Length; // kind of not needed.
        }
    }