views:

278

answers:

2

My source images are 2288px x 1712px and 816kb. When I create a thumbnail of 1024 x 768 the file sizes are more than double the high res source (1.13mb - 1.8mb depending on method used).

The source images and thumbnails created are .jpg

Even creating a 299 x 224 thumbnail is 180kb where as if I manually create a thumbnail in GIMP it is 14kb.

I need the images to look good so cannot compromise on quality for size.

I have tried Image.GetThumbnailImage which created a 1.13mb file but I read that it can create unexpected results.

Most articles say to write the code like this article: http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

That gives me a thumbnail of 1.83mb.

Should I be expecting larger file sizes compared to doing the same thing with a desktop application?

I have tried 16 bit instead of 24 and commenting out the "high quality" values but I still cannot get it under 1mb. Any ideas?

Bitmap bmPhoto = new Bitmap(destWidth, destHeight,PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(image.HorizontalResolution,image.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);

        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;        
        grPhoto.SmoothingMode = SmoothingMode.HighQuality;
        grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;

EDIT FOR ANSWER

I was lacking compression. My 1024 thumbnail is now 112kb.

Encoder qualityEncoder = Encoder.Quality;
//80L is the compression percentage
EncoderParameter ratio = new EncoderParameter(qualityEncoder, 80L);
// Add the quality parameter to the list
EncoderParameters codecParams = new EncoderParameters(1);
codecParams.Param[0] = ratio;

ImageCodecInfo jpegCodecInfo = GetEncoderInfo("image/jpeg");


bmPhoto.Save(Path.Combine(filepath, RenameImage(filename, appendName, replaceWholeString)), jpegCodecInfo, codecParams);


ImageCodecInfo GetEncoderInfo(string mimeType)    
{    
int j;    
ImageCodecInfo[] encoders;    
encoders = ImageCodecInfo.GetImageEncoders();

for(j = 0; j < encoders.Length; ++j)    
{    
if(encoders[j].MimeType.ToUpper() == mimeType.ToUpper())    
return encoders[j];    
}    
return null;    
}
+1  A: 

The bitmap format is your main problem. This will always be huge by comparison with other formats. Bitmaps are run-length-encoded and uncompressed.

If your images are photographs, JPEG is your best bet. You can get great visual quality with tiny file sizes. If they are line art and don't have a lot of color, GIF may also be a good option. There are several good image manipulation libraries out there, such as FreeImage.

EDIT: Noticed the comment that you're going JPEG to JPEG... the lack of good compression may be a limitation of the Framework's built-in image capabilities.

Dave Swersky
+2  A: 

This sounds like you're using no compression or a very weak one (like RLE). Try PNG, JPG (or JPG2000 which can be lossless) or GIF.

EDIT: If you use JPG, try to reduce image quality. 1024x768 thumbnails should be able to be a lot smaller than 1 MB and still looking good.

schnaader