Share via


Cómo: Utilizar el modo de interpolación para controlar la calidad de imagen durante el ajuste de tamaño

El modo de interpolación de un objeto Graphics influye en la manera en que GDI+ ajusta el tamaño (amplía y contrae) de las imágenes. La enumeración InterpolationMode define varios modos de interpolación, algunos de los cuales se muestran en la siguiente lista:

Para ajustar una imagen, cada píxel de la imagen original se debe asignar a un grupo de píxeles de la imagen más grande. Para encoger una imagen, se deben asignar grupos de píxeles de la imagen original a píxeles únicos de la imagen más pequeña. La efectividad de los algoritmos que realizan estas asignaciones determina la calidad de una imagen cuyo tamaño se ha ajustado. Los algoritmos que producen imágenes con el tamaño ajustado de mayor calidad suelen requerir más tiempo de procesamiento. En la lista anterior, NearestNeighbor es el modo de menor calidad y HighQualityBicubic es el modo de mayor calidad.

Para establecer el modo de interpolación, asigne uno de los miembros de la enumeración InterpolationMode a la propiedad InterpolationMode de un objeto Graphics.

Ejemplo

En el ejemplo siguiente se dibuja una imagen que posteriormente se encoge con tres modos de interpolación distintos:

En la ilustración siguiente se muestran la imagen original y las tres imágenes más pequeñas.

Imagen con diversos valores de interpolación

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

Compilar el código

El ejemplo anterior está diseñado para formularios Windows Forms y requiere PaintEventArgs e, que es un parámetro del controlador de eventos Paint.

Vea también

Otros recursos

Imágenes, mapas de bits y metarchivos

Trabajar con imágenes, mapas de bits, iconos y metarchivos