Graphics オブジェクトの補間モードは、GDI+ によるイメージの拡大縮小 (拡大と縮小) の方法に影響します。 InterpolationMode列挙では、いくつかの補間モードが定義されています。その一部を次の一覧に示します。
イメージを拡大するには、元のイメージ内の各ピクセルを、大きな画像のピクセルのグループにマップする必要があります。 イメージを縮小するには、元のイメージ内のピクセルのグループを、小さい画像の 1 つのピクセルにマップする必要があります。 これらのマッピングを実行するアルゴリズムの有効性によって、スケーリングされたイメージの品質が決まります。 より高品質のスケーリングされた画像を生成するアルゴリズムでは、処理時間が長くなる傾向があります。 上記の一覧では、 NearestNeighbor は最も低品質のモードで、 HighQualityBicubic は最高品質モードです。
補間モードを設定するには、InterpolationMode列挙体のメンバーの 1 つを、InterpolationMode オブジェクトのGraphics プロパティに割り当てます。
例
次の例では、イメージを描画し、3 つの異なる補間モードでイメージを縮小します。
次の図は、元の画像と 3 つの小さい画像を示しています。
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 フォームで使用できるように設計されており、PaintEventArgs イベント ハンドラーのパラメーターである e
Paintが必要です。
こちらも参照ください
.NET Desktop feedback