views:

88

answers:

2

I am resizing uploaded images as follows:

var bmPhoto = new Bitmap(width, height, PixelFormat.Format16bppRgb555);

using (var grPhoto = Graphics.FromImage(bmPhoto))
{
  grPhoto.SmoothingMode = SmoothingMode.HighSpeed;
  grPhoto.CompositingQuality = CompositingQuality.HighSpeed;
  grPhoto.InterpolationMode = InterpolationMode.Low;
  grPhoto.DrawImage(sourceImage, new Rectangle(destX, destY, destWidth, destHeight),
                        new Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
}

How can I limit the resulting file size so that it is no bigger than, say, 3KB? I am uploading a 1024x768 JPG which is 768KB. When resizing to 100x100 with the above code, I can't get it any smaller than 12KB.

A: 

You can try to play with the quality property, but there is a way to know whats the resulting image size. You can also try to save the file as png, which works better for non photo images (graphics)

Shay Erlichmen
I don't think I can lower the quality any more without dropping to 8-bit. Unfortunately, these are photos.
Mark Richman
@Mark Richman, if you can't lower the quality any further then the question becomes moot. 12KB is the best you are going to do at that quality level and size.
Lazarus
+1  A: 

The resize code isn't what you need to be looking at. Take a look at the jpeg compression level when you are doing your Bitmap.Save().

3kb is completely doable with a 100x100 image. If you are getting 12kb, you are most likely saving the jpeg with the highest quality jpeg compression.

Here is an MSDN article about how to set the compression level when saving the bitmap.

Alan Jackson