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

How use ImageSharp(Web) to compress / mutate stream with images(IFormFile)

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

简单代码示例:

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.
        }

我知道这个 post 有点旧,我相信你已经解决了你的问题,但希望它能在未来帮助其他人。

Can I reuse the same memory stream / IFormFile object to save the mutated image? Or do I need to create a new Image from current image object?

这里有两个流,你可以使用一个流来处理 IFormFile 中的图像,你可以在这里操作所有内容,然后使用你的 MemoryStream 保存到

To work with a memory Stream do I also need to use specific JpegDecoder()?

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

Not sure if this line is correct item.SaveAsJpeg(memoryStream);.

你走在正确的轨道上

Maybe someone can help me out with the logic or any tips or tricks would be really helpful. Thanks!

这是我根据您所拥有的重写:希望这对您有所帮助。我敢肯定有些事情可以简化,我试着让它接近 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.
        }
    }