Share via


Comment : dessiner des images avec transparence

Mise à jour : novembre 2007

Le .NET Compact Framework prend en charge la transparence, mais avec uniquement une couleur de transparence. La méthode SetColorKey(Color, Color) doit avoir la même couleur spécifiée pour les gammes de couleurs haute et basse.

Exemple

Cet exemple crée un Bitmap d'un rectangle avec des conceptions rouges et noires et illustre deux techniques permettant de définir la transparence :

  • Utilisez la méthode SetColorKey(Color, Color) basée sur un pixel dans l'image. Cet exemple définit la transparence à l'aide du pixel supérieur gauche de l'image. Dans la mesure où ce pixel est noir, tous les pixels noirs à l'origine seront transparents.

  • Utilisez la méthode SetColorKey(Color, Color) avec un paramètre de couleur explicite. Cet exemple lui affecte la valeur rouge, afin que tous les pixels rouges à l'origine soient transparents.

Pour effectuer la démonstration, exécutez d'abord l'application avec les deux techniques de transparence commentées pour afficher l'aspect de l'image lorsque la transparence n'est pas définie. Supprimez ensuite les marques de commentaire du code pour l'une ou l'autre des techniques de transparence.

' The .NET Compact Framework supports transparency,
' but with only one transparency color.
' The SetColorKey method must have the same color 
' specified for the low color and high color range.

' Note that you must uncomment lines of code
' as indicated for the desired transparency effect.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

' Create a red and black bitmap to demonstrate transparency.
    Dim bmp As New Bitmap(75, 75)
    Dim g As Graphics = Graphics.FromImage(bmp)

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


Dim attr As New ImageAttributes

' Set the transparency color key based on the upper-left pixel 
' of the image.
' Uncomment the following line to make all black pixels transparent:
' attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0))

' Set the transparency color key based on a specified value.
' Uncomment the following line to make all red pixels transparent:
' attr.SetColorKey(Color.Red, Color.Red)

' Draw the image using the image attributes.
Dim dstRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, _
    GraphicsUnit.Pixel, attr)

End Sub
// The .NET Compact Framework supports transparency,
// but with only one transparency color.
// The SetColorKey method must have the same color 
// specified for the low color and high color range.

// Note that you must uncomment lines of code
// as indicated for the desired transparency effect.

protected override void OnPaint(PaintEventArgs e)
{
    // Create a red and black bitmap to demonstrate transparency.
    Bitmap bmp = new Bitmap(75,75);
    Graphics g = Graphics.FromImage(bmp);

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

    ImageAttributes attr = new ImageAttributes();

    // Set the transparency color key based on the upper-left pixel 
    // of the image.
    // Uncomment the following line to make all black pixels transparent:
    // attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));

    // Set the transparency color key based on a specified value.
    // Uncomment the following line to make all red pixels transparent:
    // attr.SetColorKey(Color.Red, Color.Red);

    // Draw the image using the image attributes.
    Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
        GraphicsUnit.Pixel, attr);
}

Compilation du code

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

Programmation fiable

Notez que l'objet Graphics qui est utilisé pour créer la bitmap est supprimé explicitement. 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