C # why image resizing will increase file size

I have an image file that is a 6k jpg file with a width: 172px and a height: 172px.

I am using the following code, try resizing it to 128 * 128px jpg file:

public static Image ResizeImage(Image img, int width, int height) { var b = new Bitmap(width, height, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(b)) { g.DrawImage(img, 0, 0, width, height); } return b; } 

This code strangely increased the file size to 50k, can anyone explain why? and how to resize the image to 128 * 128 pixels and keep the size about 6k.

Many thanks.

Dy

+6
c # image-processing
source share
2 answers

It depends on the algorithm used to compress the jpeg file. Certain algorithms lose more (lose image quality) than others, but benefit from a smaller size.

What happens in the code, jpeg expands into a bitmap in memory. When he went to save the 128x128 jpeg, the code used an algorithm that does less compression than the one used to save the original image. This made him create a larger jpeg file, although the image size itself is smaller.

+11
source share

In the sent code, you are not returning a JPEG file, but a bitmap (128x128 24bpp uncompressed bitmap has a size of 48kB). You will have to squeeze it again, this tutorial may help.

+6
source share

All Articles