GDI+ InterpolationMode
If you are resizing images, especially JPEG images in GDI plus you should definitely play around with the Graphics object’s InterpolationMode property. If you use the default mode for resizing, especially if size down to thumbnail size from rather large images you will probably find your pictures pixelized or showing ragged lines at any hard angles in the picture. InterpolationMode will do away with this in many cases resulting in much smoother looking images.
Interpolation modes use different algorithms to resample the original image for its new size and try to render as close as possible in the new resolution. Most image manipulation programs like Photoshop and PaintShop Pro support many of the same Interpolation modes.
Here’s what I use in my ResizeImage routine I mentioned a while back for thumbnailing:
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
...
This has made a huge difference in the quality of the thumbnails which I use in my Photo Album.
Another important thing to remember is that GDI+ does best with image sizes that are multiples of 16. If you resize images try to make the height and width a multiple of 16. According to the documentation this will allow GDI+ to maintain the original image quality attributes resulting in preservation of more of the settings when resizing.
These are relatively simple tweaks that have been a big help to me and I thought I’d pass it on here…
Other Posts you might also like
The Voices of Reason
# re: GDI+ InterpolationMode cause bad results
Seems to me, that interpolation uses "black" pixels outside the picture, and thereby making the edges a bit darker.
Am I totally wrong here or is it just me?
# re: GDI+ InterpolationMode
When transforming a Graphics-object, one might want to draw a filled Rectangle in the background first using either Brushes.LightGray or some matching color. This eliminate the seemingly black edges in the top and the left. But the problem doesn't disappear anyway!
When defining the Rectangle of which to DrawImage, I decided to move the offset to -1, -1 and add 1 to the Width and Height. Now the picture is stretched a little more, but the black edges neatly disappear!
Hope someone can use my "tricks" or perhaps even shed some light on why the interpolation-work this way?
# re: GDI+ InterpolationMode
# re: GDI+ InterpolationMode
grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
grPhoto.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
Using these options in my case showed a single pixel at (0,0) which was gray and not the darker line as described above. The picture mainly had a white background. It is probably a good idea to resize the image to a multiple of 16 pixels for HighQualityBicubic resizing. It looks like a bug...
# re: GDI+ InterpolationMode
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.CompositingQuality = CompositingQuality.HighQuality;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
//NOTE: This removes fringing as the source image is not blended at the edges with
//the transparent background, preserving the magic colour for 'MakeTransparent'
grPhoto.CompositingMode = CompositingMode.SourceCopy;
# re: GDI+ InterpolationMode
We are building an Web Site where we implementing the compression Logic provided by GDI + Library for making the Thumbsnail image.
But in the process we are losing the quality of the end compressed image.
I am adding the snip shot of code which we are using..
mbmpSave = new Bitmap(mintpxlDestWidth, mintpxlDestHeight);
using (Graphics g = Graphics.FromImage(mbmpSave))
{
g.Clear(mclrBackground);
g.DrawImage(mimgOriginal, intLeftX,intLeftY, intTargetWidth, intTargetHeight);
mbmpSave.Save ("E:\\images\\08_Ex\\135.Jpeg");
}
Can we improve this behavior by using others functions provided in GDI + without degrading the performance
Any Quick help would be deeply needed and appreciated….
Regards
Haroon