tags:

views:

236

answers:

3

i have the following code to take an image and generate the thumbnail.

how can i alter the quality or compression to get smaller filesizes programatically?

Image thumbNail = image.GetThumbnailImage(Width, Height, null, new IntPtr());
+1  A: 

When saving the thumbNail with Image.Save you can specify the quality by passing a EncoderParameter. See: Reducing JPEG Picture Quality using C#

EncoderParameter epQuality = new EncoderParameter(
    System.Drawing.Imaging.Encoder.Quality,
    (int)numQual.Value);

...
newImage.Save(..., iciJpegCodec, epParameters);
dtb
+1  A: 

You do not use the GetThumbnailImage API:

protected Stream ResizeImage(string source, int width, int height) {
            using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(source))
            using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height)) 
            using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp))
            {

                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                graphic.DrawImage(bmp, 0, 0, width, height);

                MemoryStream ms = new MemoryStream();
                newBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                return ms;

            }             
        }
Sam Saffron
+1  A: 

If you truly need better control over the thumbnails produced, you will be better off producing your own by manually generating an image of smaller size and different quality. The GetThumbnailImage dows not give you much control.

See this article for how it's done. http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

David Stratton