HOW TO:縮放期間使用插補法模式控制影像品質
更新:2007 年 11 月
Graphics 物件的插補法模式會影響 GDI+ 縮放 (延展和縮小) 影像的方式。InterpolationMode 列舉型別定義了幾種插補法模式,下列清單顯示了其中幾種模式:
若要延展影像,必須將原始影像中的每一個像素對應至較大影像中的像素群組。若要縮小影像,則必須將原始影像中的像素群組對應至較小影像中的單一像素。執行這些對應的演算法有效性會決定縮放影像的品質。可產生較高品質縮放影像的演算法通常需要較長的處理時間。在上述清單中,NearestNeighbor 是品質最低的模式,HighQualityBicubic 則是品質最高的模式。
若要設定插補法模式,請指派 InterpolationMode 列舉型別的成員之一至 Graphics 物件的 InterpolationMode 屬性。
範例
下列範例會繪製影像,然後用三種不同的插補法模式來縮小影像:
下圖顯示的是原始影像和三個較小的影像。
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)
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);
編譯程式碼
上述範例是專為與 Windows Form 搭配使用而設計的,而且它需要 PaintEventArgs e (即 Paint 事件處理常式的參數)。