Condividi tramite


Procedura: utilizzare la modalità di interpolazione per controllare la qualità dell'immagine durante l'adattamento

Aggiornamento: novembre 2007

La modalità di interpolazione di un oggetto Graphics determina il modo in cui gli oggetti vengono adattati, tramite estensione o riduzione, in GDI+. Nell'enumerazione InterpolationMode sono definite numerose modalità di interpolazione, alcune delle quali sono indicate nell'elenco che segue:

Per estendere un'immagine ciascun pixel nell'immagine originale deve essere mappato rispetto a un gruppo di pixel dell'immagine più grande. Per ridurre un'immagine i gruppi di pixel dell'immagine originale devono essere mappati rispetto a singoli pixel dell'immagine più piccola. La qualità dell'immagine adattata dipende dall'efficacia degli algoritmi che eseguono le associazioni. Gli algoritmi che producono immagini adattate di qualità elevata richiedono solitamente maggiori tempi di elaborazione. Nell'elenco precedente NearestNeighbor rappresenta la modalità con qualità più bassa e HighQualityBicubic la modalità con qualità più elevata.

Per impostare la modalità di interpolazione, assegnare uno dei membri dell'enumerazione InterpolationMode alla proprietà InterpolationMode di un oggetto Graphics.

Esempio

Nell'esempio che segue viene disegnata un'immagine, che successivamente viene ridotta utilizzando tre diverse modalità di interpolazione.

Nell'illustrazione che segue sono mostrate l'immagine originale e le tre immagini più piccole.

Immagine con varie impostazioni di interpolazione

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

Compilazione del codice

L'esempio riportato in precedenza è stato creato per essere utilizzato con Windows Form e richiede PaintEventArgs e, un parametro del gestore eventi Paint.

Vedere anche

Altre risorse

Immagini, bitmap e metafile

Utilizzo di immagini, bitmap, icone e metafile