Compartilhar via


Como: Copiar imagens

O .NET Compact estrutura não dá suporte a Image.Clone método, mas você ainda pode copiar imagens e partes das imagens. Os exemplos a seguir mostram como fazer o seguinte:

  • Definir um método para criar um bitmap.

  • Definir um método sobrecarregado para copiar um bitmap ou para copiar parte de um bitmap.

  • Chamar esses métodos e desenhar as imagens na tela, substituindo o OnPaint método do seu formulário.

Para criar um bitmap

  • Esse método cria um bitmap para fins de demonstração.
' Creates a bitmap for copying.
Function CreateBitmap(sideSize As Integer) As Bitmap
    Dim bmp As New Bitmap(sideSize, sideSize)
    Dim g As Graphics = Graphics.FromImage(bmp)

    g.FillEllipse(New SolidBrush(Color.Red), 0, 0, sideSize, sideSize)
    g.DrawLine(New Pen(Color.Black), 0, 0, sideSize, sideSize)
    g.DrawLine(New Pen(Color.Black), sideSize, 0, 0, sideSize)
    g.Dispose()

    Return bmp
End Function
// Creates a bitmap for copying.
private Bitmap CreateBitmap(int sideSize)
{
    Bitmap bmp = new Bitmap(sideSize, sideSize);
    Graphics g = Graphics.FromImage(bmp);

    g.FillEllipse(new SolidBrush(Color.Red), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), 0, 0, sideSize, sideSize);
    g.DrawLine(new Pen(Color.Black), sideSize, 0, 0, sideSize);
    g.Dispose();

    return bmp;
}

Ao clonar um bitmap

  • Essa sobrecarga de método pega um bitmap fonte como um parâmetro e retorna o bitmap como uma cópia.
' Copies the entire bitmap.
Overloads Function CopyBitmap(source As Bitmap) As Bitmap
    Return New Bitmap(source)
End Function
// Copies the entire bitmap.
protected Bitmap CopyBitmap(Bitmap source)
{
    return new Bitmap(source);
}

Para copiar parte de um bitmap

  • Essa sobrecarga de método tem um Rectangle sistema autônomo um parâmetro para determinar sistema autônomo dimensões da parte do bitmap para retornar.
' Copies a part of the bitmap.
Overloads Function CopyBitmap(source As Bitmap, part As Rectangle) As Bitmap
    Dim bmp As New Bitmap(part.Width, part.Height)

    Dim g As Graphics = Graphics.FromImage(bmp)
    g.DrawImage(source, 0, 0, part, GraphicsUnit.Pixel)
    g.Dispose()

    Return bmp
End Function
// Copies a part of a bitmap.
protected Bitmap CopyBitmap(Bitmap source, Rectangle part)
{
    Bitmap bmp = new Bitmap(part.Width, part.Height);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(source,0,0,part,GraphicsUnit.Pixel);
    g.Dispose();
    return bmp;
}

Para criar, copiar, e desenhar os bitmaps

  • Este OnPaint sobrecarga de método chama os métodos para criar um bitmap e depois clonar e copiar uma parte dele. Ele também salva o bitmap clonado para um arquivo.
' Draws the bitmaps on the form.   
Protected Overrides Sub OnPaint(e As PaintEventArgs)
    Dim arialFont As Font
    Dim blackBrush As Brush

    arialFont = New Font("Arial", 10, FontStyle.Regular)
    blackBrush = New SolidBrush(Color.Black)

    ' Set the size of the sides of the bitmap,
    ' and get one-third of it for the center bitmap.
    Dim sidesize As Integer = 75
    Dim third As Integer = CInt(sidesize / 3)

    ' Create bitmap.
    Dim source As Bitmap = CreateBitmap(sidesize)

    ' Copy entirely as a clone.
    Dim clone As Bitmap = CopyBitmap(source)

    ' Copy the center part of the bitmap.
    Dim center As Bitmap = _ 
        CopyBitmap(source, New Rectangle(third, third, third, third))

    ' Save the bitmap to a file.
    clone.Save("newbitmap.bmp", ImageFormat.Bmp)

    ' Draw the source, clone, and partial 
    ' bitmaps vertically down the screen. 
    Dim y As Integer = 10

    e.Graphics.DrawString("source bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(source, 10, y)
    y += source.Height + 10

    e.Graphics.DrawString("clone bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(clone, 10, y)
    y += clone.Height + 10

    e.Graphics.DrawString("center part of bitmap:", ArialFont, BlackBrush, 10, y)
    y += 20

    e.Graphics.DrawImage(center, 10, y)
    y += center.Height + 10

    ' Dispose graphic objects.
    arialFont.Dispose()
    blackBrush.Dispose()

End Sub
// Draws the bitmaps on the form.   
protected override void OnPaint(PaintEventArgs e)
{
    Font arialFont;
    Brush blackBrush;
    arialFont = new Font("Arial", 10, FontStyle.Regular);
    blackBrush = new SolidBrush(Color.Black);

    // Set the size of the sides of the bitmap,
    // and get one-third of it for the center bitmap.
    int sidesize = 75;
    int third = (int) sidesize/3;

    // Create bitmap.
    source = CreateBitmap(sidesize);

    // Copy entirely as a clone.
    clone = CopyBitmap(source);

    // Copy the center part of the bitmap.
    center = CopyBitmap(source, new Rectangle(third, third, third, third));

    // Save the bitmap to a file.
    clone.Save("newbitmap.bmp", ImageFormat.Bmp);

    // Draw the source, clone, and partial 
    // bitmaps vertically down the screen. 
    int y = 10;

    e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(source, 10, y);
    y += source.Height + 10;

    e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(clone, 10, y);
    y += clone.Height + 10;

    e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y);
    y += 20;

    e.Graphics.DrawImage(center, 10, y);
    y += center.Height + 10;

    // Dispose graphic objects.
    arialFont.Dispose();
    blackBrush.Dispose();
}

Compilando o código

Este exemplo requer referências aos seguintes namespaces:

Programação robusta

Observe que o Font e Brush objetos são descartados explicitamente na OnPaint sobrecarga de método. The Graphics objeto retornado pela Graphics propriedade das PaintEventArgs objeto é destruído pelo coletor de lixo e não precisará ser explicitamente destruído.

Consulte também

Outros recursos

Elementos gráficos e desenho no .NET Compact estrutura