在缩放期间使用内插模式控制图像质量

图形对象的内插模式会影响 Windows GDI+ 缩放 (拉伸和收缩) 图像的方式。 Gdiplusenums.h 中的 InterpolationMode 枚举定义了多种内插模式,其中一些模式如以下列表所示:

  • InterpolationModeNearestNeighbor
  • InterpolationModeBilinear
  • InterpolationModeHighQualityBilinear
  • InterpolationModeBicubic
  • InterpolationModeHighQualityBicubic

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

若要设置内插模式,请将 InterpolationMode 枚举的成员之一传递给 Graphics 对象的 SetInterpolationMode 方法。

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

Image image(L"GrapeBunch.bmp");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
// Draw the image with no shrinking or stretching.
graphics.DrawImage(
   &image,
   Rect(10, 10, width, height),  // destination rectangle  
   0, 0,        // upper-left corner of source rectangle
   width,       // width of source rectangle
   height,      // height of source rectangle
   UnitPixel);
// Shrink the image using low-quality interpolation. 
graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);
graphics.DrawImage(
   &image,
   Rect(10, 250, 0.6*width, 0.6*height),  // destination rectangle 
   0, 0,        // upper-left corner of source rectangle
   width,       // width of source rectangle
   height,      // height of source rectangle
   UnitPixel);
// Shrink the image using medium-quality interpolation.
graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear);
graphics.DrawImage(
   &image,
   Rect(150, 250, 0.6 * width, 0.6 * height),  // destination rectangle 
   0, 0,        // upper-left corner of source rectangle
   width,       // width of source rectangle
   height,      // height of source rectangle
   UnitPixel);
// Shrink the image using high-quality interpolation.
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
graphics.DrawImage(
   &image,
   Rect(290, 250, 0.6 * width, 0.6 * height),  // destination rectangle 
   0, 0,        // upper-left corner of source rectangle
   width,       // width of source rectangle
   height,      // height of source rectangle
   UnitPixel);

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

显示大型原始图像和三个不同质量的小图像的插图