0

I use the below code to resize JPG files to 200x240 pixels. The attached example file has a size of 900x600 pixels and is only 84 kb. However when I save the resized file using ImageSharp, the produced image is 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. How can I reduce this?

enter image description here

Resized image:

enter image description here

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)
                                });
                            });
Aref Karimi
  • 1,822
  • 4
  • 27
  • 46
  • 2
    This is a really poor question. You don't indicate at any point that you are saving as a png (other than by the thumbnail extension) – James South Oct 05 '20 at 22:24

1 Answers1

3

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.

            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.

    image.Save("sample.jpeg");
Usama Safi
  • 151
  • 8
  • 1
    Did you read the API docs? I'm assuming not since your using your own filestream. – James South Oct 05 '20 at 22:24
  • 1
    So you worked on ImageSharp project, the answer to your question is that i looked there, searched for any way to change bit depth for PNGs, nothing found in a direct or easy way, maybe i missed something idk, i'm no expert here, you maybe, i just helped the way i could and gave the man something that solve his problem. – Usama Safi Oct 05 '20 at 22:46
  • 1
    The easy way is to pass the encoder as you eventually discovered. There's method behind the design. Passing a bit depth (and other options) to the Save method would result in unused parameters for difference formats. https://docs.sixlabors.com/articles/imagesharp/imageformats.html#working-with-encoders – James South Oct 05 '20 at 22:56
  • 1
    I see, well for what it worth i liked that library pretty much, great work there, spent few hours on that docs to figure out what to do, searched for keyword like (format) (depth) etc, i'm sure you understand it better and can have a better way that not include a separate filestream from outside that library, best of luck. – Usama Safi Oct 05 '20 at 23:09
  • 1
    https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.ImageExtensions.html#SixLabors_ImageSharp_ImageExtensions_Save_SixLabors_ImageSharp_Image_System_String_SixLabors_ImageSharp_Formats_IImageEncoder_ – James South Oct 06 '20 at 07:31
  • 1
    Yessss that's what I'm looking for, direct and simple, idk where was this when i was searching, it's great, thank you, now the code is simply this. var pngEncoder = new PngEncoder(); pngEncoder.ColorType = PngColorType.Rgb; image.Save("sample.png", pngEncoder); – Usama Safi Oct 06 '20 at 08:04