Partager via


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

Il peut arriver que vous souhaitiez créer une bitmap hors écran qui présente les caractéristiques suivantes :

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

  • Les couleurs ne sont pas mélangées alpha les unes avec les autres lorsque vous créez la bitmap.

  • Lorsque vous affichez la bitmap terminée, les couleurs de la bitmap sont fusionnées avec les couleurs d’arrière-plan sur l’appareil d’affichage.

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

Exemple

L’exemple suivant crée un Graphics objet basé sur un Bitmap objet. Le code utilise l’objet Graphics avec deux pinceaux semi-transparents (alpha = 160) pour peindre sur la bitmap. Le code remplit un ellipse rouge et un ellipse vert à l’aide des pinceaux semi-transparents. L’ellipse verte chevauche l’ellipse rouge, mais le vert n’est pas mélangé avec le rouge, car le mode de composition de l’objet Graphics est défini SourceCopysur .

Le code dessine deux fois la bitmap sur l’écran : une fois sur un arrière-plan blanc et une fois sur un arrière-plan coloré. Les pixels de la bitmap qui font partie des deux points de suspension ont un composant alpha de 160. Les points de suspension sont donc fusionnés avec les couleurs d’arrière-plan sur l’écran.

L’illustration suivante montre la sortie de l’exemple de code. Notez que les points de suspension sont mélangés avec l’arrière-plan, mais qu’ils ne sont pas mélangés entre eux.

Diagram showing ellipses blended with the background, not each other.

L’exemple de code contient cette instruction :

bitmapGraphics.CompositingMode = CompositingMode.SourceCopy;
bitmapGraphics.CompositingMode = CompositingMode.SourceCopy

Si vous souhaitez que les points de suspension soient fusionnés les uns avec les autres, ainsi qu’avec l’arrière-plan, remplacez cette instruction par ce qui suit :

bitmapGraphics.CompositingMode = CompositingMode.SourceOver;
bitmapGraphics.CompositingMode = CompositingMode.SourceOver

L’illustration suivante montre la sortie du code révisé.

Diagram that shows ellipses blended together and with background.

// 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);
' 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)

Compilation du code

L'exemple précédent est conçu pour une utilisation avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre de PaintEventHandler.

Voir aussi