Simplified Image Processing in GDI+ - Sample Three-Use Interpolation to Output
(Page 5 of 6 )
Different Qualities of Images
What is interpolation? Interpolation is a kind of mathematic algorithm to compute the middle value of two given endpoints.
In GDI+ you can use Interpolation to adjust the rendering quality of an image. If you want to stretch an image, you have to map each of the pixels in the source image to the corresponding pixel in the larger target one, while achieving a shrink effect requires similar mapping with the smaller target image. As you may image, the selected mapping algorithm will decide the result of the rendered image. To gain better quality requires longer processing time.
An enumeration type, InterpolationMode, is defined in GDI+ to control the different interpolation modes. The following table lists all the values associated with InterpolationMode.
Member name | Description |
Invalid | Equivalent to the Invalid element of the QualityMode enumeration. |
Default | Specifies default mode. |
Low | Specifies low quality interpolation. |
High | Specifies high quality interpolation. |
Bilinear | Specifies bilinear interpolation. No prefiltering is done. This mode is not suitable for shrinking an image below 50 percent of its original size. |
Bicubic | Specifies bicubic interpolation. No prefiltering is done. This mode is not suitable for shrinking an image below 25 percent of its original size. |
NearestNeighbor | Specifies nearest-neighbor interpolation. |
HighQualityBilinear | Specifies high-quality, bilinear interpolation. Prefiltering is performed to ensure high-quality shrinking. |
HighQualityBicubic | Specifies high-quality, bicubic interpolation. Prefiltering is performed to ensure high-quality shrinking. This mode produces the highest quality transformed images. |
Now let's look at a sample to see how you can use the different interpolation modes to affect rendering quality. Figure 3 illustrates the running-time snapshot for sample 3, through which you can apparently notice from the head and feature parts of the eagle that different modes of interpolation result in different rendering quality.
Figure 3-the running-time snapshot for sample 3

You can find the final answer in the following code:
private void UsingInterpolationMode_Click(object sender, System.EventArgs e)
{
Graphics graphics=this.CreateGraphics();
graphics.Clear(Color.White);
//load the image
Bitmap image=new Bitmap("eagle.bmp");
int width = image.Width;
int height = image.Height;
//draw the source image
graphics.DrawImage(
image,
new Rectangle(0, 0, width, height), //target area
0, 0, //upper left corner of the source image
width, //width of the source image
height, //height of the source image
GraphicsUnit.Pixel);
//shift to drawing plane to the right
graphics.TranslateTransform( width+10,0);
//use NearestNeighbor (low quality)
graphics.InterpolationMode=InterpolationMode.NearestNeighbor;
graphics.DrawImage(
image,
new RectangleF(0.0f, 0.0f, 0.6f * width, 0.6f * height), //target area
new RectangleF(0, 0, //upper left corner of the source image
width, //width of the source image
height), //height of the source image
GraphicsUnit.Pixel);
//shift to drawing plane to the right
graphics.TranslateTransform( 0.6f*width+10,0);
// use Bicubic mode (high quality)
graphics.InterpolationMode=InterpolationMode.Bicubic;
graphics.DrawImage(
image,
new RectangleF(0, 0, 0.6f * width, 0.6f * height), //target area
new Rectangle(0, 0, //upper left corner of the source image
width, //width of the source image
height), //height of the source image
GraphicsUnit.Pixel);
//shift to drawing plane to the right
graphics.TranslateTransform(0.6f*width+10,0f);
// use the highest quality of HighQualityBicubic mode
graphics.InterpolationMode=InterpolationMode.HighQualityBicubic;
graphics.DrawImage(
image,
new RectangleF(0, 0, 0.6f * width, 0.6f * height), //target area
new Rectangle(0, 0, //upper left corner of the source image
width, //width of the source image
height), //height of the source image
GraphicsUnit.Pixel);
}
There is also one point worth noticing: different modes of interpolation will exhaust different quantities of system resources. And from Figure 3 above you will have concluded that the HighQualityBicubic mode of interpolation will consume the most system resources, which of course corresponds to the slowest rendering speed.
Next: Sample Four-Flipping an Image >>
More Windows Scripting Articles
More By Xianzhong Zhu