gpt4 book ai didi

c# - 如何使用 ImageSharp(Web) 用图像压缩/变异流(IFormFile)

转载 作者:行者123 更新时间:2023-12-03 14:24:47 26 4
gpt4 key购买 nike

我正在尝试使用 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.
    }

    最佳答案

    我知道这篇文章有点旧,我相信你已经解决了你的问题,但希望它会在 future 帮助其他人。

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

    关于c# - 如何使用 ImageSharp(Web) 用图像压缩/变异流(IFormFile),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57449487/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com