Menggunakan Mode Interpolasi untuk Mengontrol Kualitas Gambar Selama Penskalaan

Mode interpolasi objek Grafis memengaruhi cara Windows GDI+ menskalakan (meregangkan dan menyusutkan) gambar. Enumerasi InterpolationMode di Gdiplusenums.h mendefinisikan beberapa mode interpolasi, beberapa di antaranya ditunjukkan dalam daftar berikut:

  • InterpolationModeNearestNeighbor
  • InterpolationModeBilinear
  • InterpolationModeHighQualityBilinear
  • InterpolationModeBicubic
  • InterpolationModeHighQualityBicubic

Untuk meregangkan gambar, setiap piksel dalam gambar asli harus dipetakan ke sekelompok piksel dalam gambar yang lebih besar. Untuk menyusutkan gambar, grup piksel dalam gambar asli harus dipetakan ke satu piksel dalam gambar yang lebih kecil. Efektivitas algoritma yang melakukan pemetaan ini menentukan kualitas gambar berskala. Algoritma yang menghasilkan gambar berskala berkualitas lebih tinggi cenderung membutuhkan lebih banyak waktu pemrosesan. Dalam daftar sebelumnya, InterpolationModeNearestNeighbor adalah mode berkualitas terendah dan InterpolationModeHighQualityBicubic adalah mode berkualitas tinggi.

Untuk mengatur mode interpolasi, berikan salah satu anggota enumerasi InterpolationMode ke metode SetInterpolationMode dari objek Grafis .

Contoh berikut menggambar gambar lalu menyusutkan gambar dengan tiga mode interpolasi yang berbeda:

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);

Ilustrasi berikut menunjukkan gambar asli dan tiga gambar yang lebih kecil.

ilustrasi memperlihatkan gambar asli yang besar, dan tiga gambar yang lebih kecil dengan kualitas yang bervariasi