Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more

GDI+ InterpolationMode


:P
On this page:

 

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…


The Voices of Reason


 

Haroon
November 16, 2005

# re: GDI+ InterpolationMode

Hi All,



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


Jacob Kamp Hansen
December 09, 2005

# re: GDI+ InterpolationMode cause bad results

During my work with GDI+ (in ASP.NET / C#) I have experienced a particular bad result when using eg. InterpolationMode.High or .HighQualityBicubic. The resulting picture contains a darker line across the top and one at the left side of the picture, making the picture not quite as attractive as a Photoshop-handled photo.

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?

Jacob Kamp Hansen
December 09, 2005

# re: GDI+ InterpolationMode

Found a couple of solutions ...

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?

Rick Strahl's Web Log
October 15, 2006

# Creating Thumbnail Images on the fly with ASP.Net - Rick Strahl's Web Log


Kevin Thomas
March 10, 2007

# re: GDI+ InterpolationMode

Why can't people put complete (and tested) code up as examples etc. I have been trying for months to display raw data (telemetry) at a decent frame rate. Can do it with Borland C Builder (one evenings work). Using Visual Studio 2005 - a disaster. How can I take 32768 bytes and blat it to a 128 x 128 bitmap scaled to 384 x 384 pixels and at least 20 times a second? The scaling must NOT blur the image!! Oh, using C++ only.

Michael Lynn
May 09, 2008

# re: GDI+ InterpolationMode

It seems that the black line can be minimised by also using the following options:

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...

Richard Armstrong
July 28, 2009

# re: GDI+ InterpolationMode

I have found the following settings to work, this removes both the line at the top and bottom of the image, plus any fringing from using 'MakeTransparent'

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;

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2020