简体   繁体   English

SixLabors ImageSharp 生成巨大的图像文件

[英]SixLabors ImageSharp produces huge image files

I use the below code to resize JPG files to 200x240 pixels.我使用以下代码将 JPG 文件大小调整为 200x240 像素。 The attached example file has a size of 900x600 pixels and is only 84 kb.所附示例文件的大小为 900x600 像素,只有 84 kb。 However when I save the resized file using ImageSharp, the produced image is 435 kb!但是,当我使用 ImageSharp 保存调整大小的文件时,生成的图像为 435 kb! Why is the smaller image has a bigger file size?为什么较小的图像具有较大的文件大小?

I noticed that the bit depth of the original image is 24 bits but ImageSharp stores it with 32 bits.我注意到原始图像的位深度为 24 位,但 ImageSharp 将其存储为 32 位。 How can I reduce this?我怎样才能减少这种情况?

在此处输入图片说明

Resized image:调整大小的图像:

在此处输入图片说明

var thumbImage = image.Clone(x => x.GetCurrentSize());

                        if (thumbImage.Width > 200)
                        {
                            thumbImage.Mutate(m =>
                            {
                                m.Resize(new ResizeOptions
                                {
                                    Mode = ResizeMode.Min,
                                    Size = new Size(200, 0)
                                });
                            });

It took me some time to figure out how to do it for a bit-depth of 24, i wish they extended the Save method with more obvious parameters, anyway here is the code.我花了一些时间来弄清楚如何为 24 位深度做这件事,我希望他们用更明显的参数扩展 Save 方法,无论如何这里是代码。

            image.Mutate(x => x
                 .Resize(new ResizeOptions
                 {
                     Mode = ResizeMode.Min,
                     Size = new Size(240,200)
                 }));
            Stream stream = new System.IO.FileStream("sample.png", FileMode.Create);
            var pngEncoder = new SixLabors.ImageSharp.Formats.Png.PngEncoder();
            pngEncoder.ColorType = SixLabors.ImageSharp.Formats.Png.PngColorType.Rgb;

            image.SaveAsPng(stream, pngEncoder);
            stream.Dispose();

if you save as jpeg(like the original image you have here) you will get way smaller image size, and bit-depth will stay the same as the source.如果您保存为 jpeg(就像您在此处拥有的原始图像),您将获得更小的图像尺寸,并且位深度将与源保持相同。

    image.Save("sample.jpeg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM