Partager via


Comment : utiliser le mode de composition pour commander la fusion alpha

Il convient parfois de créer une bitmap hors écran ayant les caractéristiques suivantes :

  • Les couleurs ont des valeurs alpha inférieures à 255.

  • Les couleurs ne font pas l'objet d'une fusion alpha entre elles lors de la création de la bitmap.

  • Lorsque vous affichez la bitmap terminée, les couleurs dans la bitmap subissent une fusion alpha avec les couleurs d'arrière-plan sur le périphérique d'affichage.

Pour créer une telle bitmap, construisez un objet Bitmap vide, puis un objet Graphics basé sur cette bitmap. Pour l'objet Graphics, choisissez le mode de composition CompositingMode.SourceCopy.

Exemple

L'exemple suivant crée un objet Graphics basé sur un objet Bitmap. Le code utilise l'objet Graphics avec deux pinceaux translucides (alpha = 160) pour peindre sur la bitmap. Le code remplit une ellipse rouge et une ellipse verte en utilisant les pinceaux translucides. L'ellipse verte chevauche l'ellipse rouge, mais le vert ne se fond pas au rouge parce que le mode de composition de l'objet Graphics correspond à SourceCopy.

Le code dessine la bitmap à l'écran deux fois : une fois sur un arrière-plan blanc, une autre sur un arrière-plan multicolore. Les pixels dans la bitmap qui font partie des deux ellipses ont un composant alpha de 160 et se fondent donc aux couleurs d'arrière-plan sur l'écran.

L'illustration suivante montre la sortie de l'exemple de code. Notez que les ellipses se fondent à l'arrière-plan, mais ne se fondent pas entre elles.

Copie du code source

L'exemple de code contient cette instruction :

        bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;

Si vous souhaitez que les ellipses se fondent entre elles comme à l'arrière-plan, changez cette instruction de la façon suivante :

        bitmapGraphics.CompositingMode = CompositingMode.SourceOver

bitmapGraphics.CompositingMode = CompositingMode.SourceOver;

L'illustration suivante montre la sortie du code modifié.

Vue d'ensemble du code source

        ' Create a blank bitmap.
        Dim myBitmap As New Bitmap(180, 100)

        ' Create a Graphics object that we can use to draw on the bitmap.
        Dim bitmapGraphics As Graphics = Graphics.FromImage(myBitmap)

        ' Create a red brush and a green brush, each with an alpha value of 160.
        Dim redBrush As New SolidBrush(Color.FromArgb(160, 255, 0, 0))
        Dim greenBrush As New SolidBrush(Color.FromArgb(160, 0, 255, 0))

        ' Set the compositing mode so that when we draw overlapping ellipses,
        ' the colors of the ellipses are not blended.
        bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

        ' Fill an ellipse using a red brush that has an alpha value of 160.
        bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70)

        ' Fill a second ellipse using a green brush that has an alpha value of 
        ' 160. The green ellipse overlaps the red ellipse, but the green is not 
        ' blended with the red.
        bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70)

        'Set the compositing quality of the form's Graphics object. 
        e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected

        ' Draw a multicolored background.
        Dim colorBrush As New SolidBrush(Color.Aqua)
        e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100)
        colorBrush.Color = Color.Yellow
        e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100)
        colorBrush.Color = Color.Fuchsia
        e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100)

        'Display the bitmap on a white background.
        e.Graphics.DrawImage(myBitmap, 0, 0)

        ' Display the bitmap on a multicolored background.
        e.Graphics.DrawImage(myBitmap, 200, 0)

// Create a blank bitmap.
Bitmap myBitmap = new Bitmap(180, 100);

// Create a Graphics object that we can use to draw on the bitmap.
Graphics bitmapGraphics = Graphics.FromImage(myBitmap);

// Create a red brush and a green brush, each with an alpha value of 160.
SolidBrush redBrush = new SolidBrush(Color.FromArgb(160, 255, 0, 0));
SolidBrush greenBrush = new SolidBrush(Color.FromArgb(160, 0, 255, 0));

// Set the compositing mode so that when we draw overlapping ellipses,
// the colors of the ellipses are not blended.
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;

// Fill an ellipse using a red brush that has an alpha value of 160.
bitmapGraphics.FillEllipse(redBrush, 0, 0, 150, 70);

// Fill a second ellipse using a green brush that has an alpha value of 160. 
// The green ellipse overlaps the red ellipse, but the green is not 
// blended with the red.
bitmapGraphics.FillEllipse(greenBrush, 30, 30, 150, 70);

// Set the compositing quality of the form's Graphics object. 
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;

// Draw a multicolored background.
SolidBrush colorBrush = new SolidBrush(Color.Aqua);
e.Graphics.FillRectangle(colorBrush, 200, 0, 60, 100);
colorBrush.Color = Color.Yellow;
e.Graphics.FillRectangle(colorBrush, 260, 0, 60, 100);
colorBrush.Color = Color.Fuchsia;
e.Graphics.FillRectangle(colorBrush, 320, 0, 60, 100);

// Display the bitmap on a white background.
e.Graphics.DrawImage(myBitmap, 0, 0);

// Display the bitmap on a multicolored background.
e.Graphics.DrawImage(myBitmap, 200, 0);

Compilation du code

L'exemple précédent est destiné à une utilisation avec Windows Forms et nécessite PaintEventArgs e, qui est un paramètre de PaintEventHandler.

Voir aussi

Référence

FromArgb

Autres ressources

Fusion alpha de lignes et de remplissages