如何:缩放时使用插值模式控制图像质量

Graphics 对象的内插模式会影响 GDI+ 缩放(拉伸和收缩)图像的方式。 InterpolationMode 枚举定义了几种内插模式,其中一些显示在以下列表中:

若要拉伸图像,必须将原始图像中的每个像素映射到较大图像中的一组像素。 若要收缩图像,必须将原始图像中的像素组映射到较小图像中的单个像素。 执行这些映射的算法的有效性决定了缩放图像的质量。 生成更高质量缩放图像的算法往往需要更多的处理时间。 在前面的列表中,NearestNeighbor 是最低质量模式,HighQualityBicubic 是最高质量模式。

若要设置内插模式,请将 InterpolationMode 枚举的成员之一分配给 Graphics 对象的 InterpolationMode 属性。

示例

以下示例绘制图像,然后使用三种不同的内插模式收缩图像。

下图显示了原始图像和三个较小的图像。

Screenshot that shows an image with varied interpolation settings.

Image image = new Bitmap("GrapeBunch.bmp");
int width = image.Width;
int height = image.Height;

// Draw the image with no shrinking or stretching.
e.Graphics.DrawImage(
    image,
    new Rectangle(10, 10, width, height),  // destination rectangle
    0,
    0,           // upper-left corner of source rectangle
    width,       // width of source rectangle
    height,      // height of source rectangle
    GraphicsUnit.Pixel,
    null);

// Shrink the image using low-quality interpolation.
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.DrawImage(
   image,
    new Rectangle(10, 250, (int)(0.6 * width), (int)(0.6 * height)),
    // destination rectangle
    0,
    0,           // upper-left corner of source rectangle
    width,       // width of source rectangle
    height,      // height of source rectangle
    GraphicsUnit.Pixel);

// Shrink the image using medium-quality interpolation.
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.DrawImage(
    image,
    new Rectangle(150, 250, (int)(0.6 * width), (int)(0.6 * height)),
    // destination rectangle
    0,
    0,           // upper-left corner of source rectangle
    width,       // width of source rectangle
    height,      // height of source rectangle
    GraphicsUnit.Pixel);

// Shrink the image using high-quality interpolation.
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.DrawImage(
    image,
    new Rectangle(290, 250, (int)(0.6 * width), (int)(0.6 * height)),
    // destination rectangle
    0,
    0,           // upper-left corner of source rectangle
    width,       // width of source rectangle
    height,      // height of source rectangle
    GraphicsUnit.Pixel);
Dim image As New Bitmap("GrapeBunch.bmp")
Dim width As Integer = image.Width
Dim height As Integer = image.Height

' Draw the image with no shrinking or stretching. Pass in the destination
' rectangle (2nd argument), the upper-left corner (3rd and 4th arguments),
' width (5th argument),  and height (6th argument) of the source 
' rectangle.
e.Graphics.DrawImage( _
    image, _
    New Rectangle(10, 10, width, height), _
    0, _
    0, _
    width, _
    height, _
    GraphicsUnit.Pixel, _
    Nothing)

' Shrink the image using low-quality interpolation. 
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor

' Pass in the destination rectangle, and the upper-left corner, width, 
' and height of the source rectangle as above.
e.Graphics.DrawImage( _
image, _
New Rectangle(10, 250, CInt(0.6 * width), CInt(0.6 * height)), _
0, _
0, _
width, _
height, _
GraphicsUnit.Pixel)

' Shrink the image using medium-quality interpolation.
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear

' Pass in the destination rectangle, and the upper-left corner, width, 
' and height of the source rectangle as above.
e.Graphics.DrawImage( _
image, _
New Rectangle(150, 250, CInt(0.6 * width), _
CInt(0.6 * height)), _
0, _
0, _
width, _
height, _
GraphicsUnit.Pixel)

' Shrink the image using high-quality interpolation.
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic

' Pass in the destination rectangle, and the upper-left corner, width, 
' and height of the source rectangle as above.
e.Graphics.DrawImage( _
    image, _
    New Rectangle(290, 250, CInt(0.6 * width), CInt(0.6 * height)), _
    0, _
    0, _
    width, _
    height, _
    GraphicsUnit.Pixel)

编译代码

前面的示例专用于 Windows 窗体,它需要 PaintEventArgse,后者是 Paint 事件处理程序的参数。

另请参阅