Share via


Comment : copier des images

Mise à jour : novembre 2007

Le .NET Compact Framework ne prend pas en charge la méthode Image.Clone, mais vous pouvez toujours copier des images et des parties d'images. Les exemples ci-dessous montrent comment effectuer les actions suivantes :

  • Définissez une méthode pour créer une bitmap.

  • Définissez une méthode surchargée pour copier une bitmap ou une partie d'une bitmap.

  • Appelez ces méthodes et dessinez les images à l'écran en substituant la méthode OnPaint de votre formulaire.

Pour créer une bitmap

  • Cette méthode crée une bitmap à des fins de démonstration.
' 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;
}

Pour cloner une bitmap

  • Cette surcharge de méthode prend une bitmap source comme un paramètre et retourne la bitmap comme une copie.
' 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);
}

Pour copier une partie d'une bitmap

  • Cette surcharge de méthode prend un Rectangle comme un paramètre pour déterminer les dimensions de la partie de la bitmap à retourner.
' 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;
}

Pour créer, copier et dessiner les bitmaps

  • Cette surcharge de méthode OnPaint appelle les méthodes pour créer une bitmap, puis cloner et copier une partie de celle-ci. Elle enregistre également la bitmap clonée dans un fichier.
' 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();
}

Compilation du code

Cet exemple nécessite des références aux espaces de noms suivants :

Programmation fiable

Notez que les objets Font et Brush sont supprimés explicitement dans la surcharge de méthode OnPaint. L'objet Graphics retourné par la propriété Graphics de l'objet PaintEventArgs est détruit par le garbage collector. Il est donc inutile de le supprimer explicitement.

Voir aussi

Autres ressources

Graphiques et dessins dans le .NET Compact Framework