Freigeben über


Vorgehensweise: Verwenden des Mischmodus zum Steuern des Alphablendings

Möglicherweise möchten Sie eine Bitmap außerhalb des Bildschirms mit den folgenden Merkmalen erstellen:

  • Die Farben weisen Alphawerte auf, die kleiner als 255 sind.

  • Auf die Farben wird beim Erstellen der Bitmap kein Alphablending angewandt.

  • Beim Anzeigen der fertigen Bitmap erfolgt ein Alphablending der Farben in der Bitmap mit den Hintergrundfarben auf dem Anzeigegerät.

Um eine solche Bitmap zu erstellen, erstellen Sie ein leeres Bitmap-Objekt und dann ein Graphics-Objekt basierend auf dieser Bitmap. Legen Sie den Mischmodus des Graphics-Objekts auf CompositingMode.SourceCopy fest.

Beispiel

Im folgenden Beispiel wird ein Graphics-Objekt basierend auf einem Bitmap-Objekt erstellt. Der Code verwendet das Graphics-Objekt zusammen mit zwei halbtransparenten Pinseln (Alpha = 160), um in der Bitmap zu zeichnen. Der Code füllt eine rote und eine grüne Ellipse mit den halbtransparenten Pinseln aus. Die grüne Ellipse überlappt die rote Ellipse, aber das Grün wird nicht mit dem Rot gemischt, da der Mischmodus des Graphics-Objekts auf SourceCopy festgelegt ist.

Der Code zeichnet die Bitmap zweimal auf dem Bildschirm: einmal auf einem weißen Hintergrund und einmal auf einem mehrfarbigen Hintergrund. Die Pixel in der Bitmap, die zu beiden Ellipsen gehören, weisen eine Alphakomponente von 160 auf, sodass die Ellipsen mit den Hintergrundfarben auf dem Bildschirm gemischt werden.

In der folgenden Abbildung ist das Ergebnis des Codebeispiels dargestellt. Beachten Sie, dass die Ellipsen mit dem Hintergrund, aber nicht miteinander gemischt werden.

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

Das Codebeispiel enthält diese Anweisung:

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

Wenn die Ellipsen miteinander und mit dem Hintergrund gemischt werden sollen, ändern Sie diese Anweisung wie folgt:

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

In der folgenden Abbildung ist das Ergebnis des überarbeiteten Codes dargestellt.

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)

Kompilieren des Codes

Das obige Beispiel ist für die Verwendung in Windows Forms konzipiert und erfordert die PaintEventArgse-Klasse, die ein Parameter von PaintEventHandler ist.

Siehe auch