fschultz / NetImageLibrary

Library to help with image handling such as resizing, cropping, applying filters (brightess, contrast, unsharpen mask, chroma key etc), watermarking, thumbnail creation, opening/saving files or streams and more.
http://kaliko.com/image-library/
Other
90 stars 27 forks source link

png bit depth settings #16

Open quaider opened 7 years ago

quaider commented 7 years ago

my png's bit depth is 24, but SavePng method is 32, which increase the size of the image

fschultz commented 7 years ago

Internally the library works with rgba-format. Unfortunately the PNG-encoder in GDI+ ignore the Encoder.ColorDepth parameter which otherwise would have been a good idea to use. The solution is to copy the bitmap image onto a new canvas with the required pixel format and then saving it:

    // Assuming that "image" is a KalikoImage object
    Bitmap target = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    Graphics g = Graphics.FromImage(target);
    g.DrawImage(image.GetAsBitmap(), new Point(0, 0));
    target.Save("24bit.png", ImageFormat.Png);

Adding a pixel format aware save function would be a good feature to add, so I'm adding this to the backlog.

quaider commented 6 years ago

thanks @fschultz , is there any way to make ColorDepth self-adaption , because not all the png is 24 bit, right?