Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
O modo de interpolação de um objeto Graphics influencia a maneira como o GDI+ escala (alonga e encolhe) imagens. A enumeração InterpolationMode define vários modos de interpolação, alguns dos quais são mostrados na lista a seguir:
Para esticar uma imagem, cada pixel na imagem original deve ser mapeado para um grupo de pixels na imagem maior. Para reduzir uma imagem, grupos de pixels na imagem original devem ser mapeados para pixels únicos na imagem menor. A eficácia dos algoritmos que executam esses mapeamentos determina a qualidade de uma imagem dimensionada. Algoritmos que produzem imagens dimensionadas de maior qualidade tendem a exigir mais tempo de processamento. Na lista anterior, NearestNeighbor é o modo de menor qualidade e HighQualityBicubic é o modo de maior qualidade.
Para definir o modo de interpolação, atribua um dos membros da enumeração InterpolationMode à propriedade InterpolationMode de um objeto Graphics.
Exemplo
O exemplo a seguir desenha uma imagem e, em seguida, reduz a imagem com três modos de interpolação diferentes.
A ilustração a seguir mostra a imagem original e as três imagens menores.
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)
Compilando o código
O exemplo anterior foi projetado para uso com o Windows Forms e requer PaintEventArgse
, que é um parâmetro do manipulador de eventos Paint.
Ver também
.NET Desktop feedback