0
votes

How to reduce the size of images to 40 KB while maintaining quality, and the dimensions of images are greater than 400 px , in C# ?

Note, was used:

  • image.GetThumbnailImage : (The size is appropriate but the dimensions are small, and the larger the dimensions the bigger the size)

  • Drawing : (Large size, larger than 700 KB)

Code:

System.Drawing.Bitmap newImage = new System.Drawing.Bitmap(newWidth, newHeight); 
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(newImage))
{
    gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
    gr.DrawImage(im3age, new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), new System.Drawing.Size(newWidth, newHeight)));
    newImage.Save(FullPhat, System.Drawing.Imaging.ImageFormat.Jpeg);
}
1
what is wrong with the second approach? is your problem that the scaled image have a different aspect ratio (how do you calculate newWidth and newHeight? Try PNG instead of Jpeg, it uses lossless compression and still achieves good file sizes. Consider an algorithm like the one that tinypng uses, there is no way to maintain arbitrary high quality targets and arbitrarily low size constraints at the same time. - Cee McSharpface
This isn't a programming issue. It's an image format issue. A PNG image at 400x400 will result in a file that's going to be roughly 600 kB, so a JPG is probably going to be the way to go. The problem with that approach is that JPG is lossy, so depending on the compression level you choose, you could see some significant losses in quality (and to get the file size down to below 40kb, you might have to pick a high compression level). It also depends on the nature of the image itself, though. Could you show us an example? - Abion47
I see in many sites when uploading large size photos, they are compressed to a size not more than 40KB and the dimensions of the image are large, larger than 400 px and at the same time the picture is clear, my question is how to get a clear picture and size no more than 40KB? - Ahmad Mody

1 Answers

1
votes

Your solution is going to be based purely on what image format you choose, what compression options you select, and most importantly, the nature of the image itself.

The popular image formats for this task would be either JPEG or PNG. Both of those formats use, among other things, clusters of color in the images to achieve their compression. That means if your image is largely the same or similar colors, you will get small file sizes, and if your image has a lot of distributed colors, the file sizes are going to be larger.

Here are some examples:

(Images saved with Photoshop. PNGs saved using highest compression levels, JPEGs saved using default level of quality "8", highest level of quality "12", and lowest level of quality "1".)

Single color

  • PNG: 4 KB
  • JPEG (8): 9 KB
  • JPEG (12): 15 KB
  • JPEG (1): 5 KB

Pure noise

  • PNG: 141 KB
  • JPEG (8): 378 KB
  • JPEG (12): 590 KB
  • JPEG (1): 146 KB

Nature

  • PNG: 341 KB
  • JPEG (8): 84 KB
  • JPEG (12): 274 KB
  • JPEG (1): 22 KB

As you can see, different kinds of images at the same resolution produce wildly different file sizes, and JPEG is not always better than PNG or vice versa. Some of the JPEG images dip below your criteria, but keep in mind that they are saved with essentially the poorest quality settings, so the image is going to come out looking like garbage.

The general rule, then, is that a 400x400 px image, saved with perfect lossless quality, in a file under 40 KB... unless the image is mostly a single flat color, that isn't going to happen.