Como: Imagens de escala e cortar

The Graphics classe fornece vários DrawImage métodos, alguns deles tiverem parâmetros de retângulo fonte e destino que você pode usar para cortar e escala de imagens.

Exemplo

O exemplo seguinte constrói um Image objeto de arquivo em disco Apple.gif. O código desenha a imagem inteira da maçã em seu tamanho original.O código chama o DrawImage método de um Graphics objeto para desenhar uma parte da imagem maçã em um retângulo de destino for maior do que a imagem original de macieiras.

The DrawImage método determina qual parte do apple para desenhar, observando o retângulo de fonte, que é especificado pelo terceiro, quarto, quinto e sexto argumentos. Nesse caso, a maçã é cortada com 75 por cento de sua largura e com 75 por cento de sua altura.

The DrawImage método determina onde desenhar a maçã cortada e o tamanho máximo para fazer a maçã cortada, observando o retângulo de destino, que é especificado pelo segundo argumento. Nesse caso, o retângulo de destino é 30 % mais largo e 30 por cento mais alto que a imagem original.

A ilustração a seguir mostra a maçã original e a maçã redimensionada e cortada.

Dim image As New Bitmap("Apple.gif")

' Draw the image unaltered with its upper-left corner at (0, 0).
e.Graphics.DrawImage(image, 0, 0)

' Make the destination rectangle 30 percent wider and
' 30 percent taller than the original image.
' Put the upper-left corner of the destination
' rectangle at (150, 20).
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim destinationRect As New RectangleF( _
    150, _
    20, _
    1.3F * width, _
    1.3F * height)

' Draw a portion of the image. Scale that portion of the image
' so that it fills the destination rectangle.
Dim sourceRect As New RectangleF(0, 0, 0.75F * width, 0.75F * height)
e.Graphics.DrawImage( _
    image, _
    destinationRect, _
    sourceRect, _
    GraphicsUnit.Pixel)

Image image = new Bitmap("Apple.gif");

// Draw the image unaltered with its upper-left corner at (0, 0).
e.Graphics.DrawImage(image, 0, 0);

// Make the destination rectangle 30 percent wider and
// 30 percent taller than the original image.
// Put the upper-left corner of the destination
// rectangle at (150, 20).
int width = image.Width;
int height = image.Height;
RectangleF destinationRect = new RectangleF(
    150,
    20,
    1.3f * width,
    1.3f * height);

// Draw a portion of the image. Scale that portion of the image
// so that it fills the destination rectangle.
RectangleF sourceRect = new RectangleF(0, 0, .75f * width, .75f * height);
e.Graphics.DrawImage(
    image,
    destinationRect,
    sourceRect,
    GraphicsUnit.Pixel);

Compilando o código

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler.Certifique-se de substituir Apple.gif com um nome de arquivo de imagem e caminho válido no seu sistema.

Consulte também

Outros recursos

Imagens, Bitmaps e metarquivos

Trabalhando com imagens, Bitmaps, ícones e metarquivos