Gewusst wie: Verwenden einer Farbmatrix zum Festlegen von Alphawerten in Bildern

Aktualisiert: November 2007

Die Bitmap-Klasse (die von der Image-Klasse erbt) und die ImageAttributes-Klasse bieten Funktionen zum Abrufen und Festlegen von Pixelwerten. Mithilfe der ImageAttributes-Klasse können Sie die Alphawerte für ein vollständiges Bild ändern. Alternativ können Sie auch die SetPixel-Methode der Bitmap-Klasse aufrufen, um einzelne Pixelwerte zu ändern.

Beispiel

Die ImageAttributes-Klasse verfügt über zahlreiche Eigenschaften, mit denen Sie Bilder beim Rendern ändern können. Im folgenden Beispiel wird für alle Alphawerte mithilfe eines ImageAttributes-Objekts 80 Prozent ihres Originalwerts festgelegt. Dazu wird eine Farbmatrix initialisiert und als Wert für die Alphaskalierung in der Matrix 0,8 festgelegt. Die Adresse der Farbmatrix wird an die SetColorMatrix-Methode des ImageAttributes-Objekts und das ImageAttributes-Objekt an die DrawString-Methode des Graphics-Objekts übergeben.

Beim Rendern werden die Alphawerte in der Bitmap in den 80-prozentigen Wert des Originals konvertiert. Dies ergibt ein Bild, dessen Farben mit den Hintergrundfarben gemischt sind. Wie in der folgenden Abbildung dargestellt, ist das Bitmapbild transparent; die durchgehende schwarze Linie scheint also durch.

In Bereichen, in denen sich das Bild über dem weißen Bereich des Hintergrunds befindet, wurde das Bild mit der Farbe Weiß gemischt. Dort, wo das Bild auf der schwarzen Linie liegt, wird es mit der Farbe Schwarz gemischt.

' Create the Bitmap object and load it with the texture image.
Dim bitmap As New Bitmap("Texture.jpg")

' Initialize the color matrix.
' Note the value 0.8 in row 4, column 4.
Dim matrixItems As Single()() = { _
   New Single() {1, 0, 0, 0, 0}, _
   New Single() {0, 1, 0, 0, 0}, _
   New Single() {0, 0, 1, 0, 0}, _
   New Single() {0, 0, 0, 0.8F, 0}, _
   New Single() {0, 0, 0, 0, 1}}

Dim colorMatrix As New ColorMatrix(matrixItems)

' Create an ImageAttributes object and set its color matrix.
Dim imageAtt As New ImageAttributes()
imageAtt.SetColorMatrix( _
   colorMatrix, _
   ColorMatrixFlag.Default, _
   ColorAdjustType.Bitmap)

' First draw a wide black line.
e.Graphics.DrawLine( _
   New Pen(Color.Black, 25), _
   New Point(10, 35), _
   New Point(200, 35))

' Now draw the semitransparent bitmap image.
Dim iWidth As Integer = bitmap.Width
Dim iHeight As Integer = bitmap.Height

' Pass in the destination rectangle (2nd argument) and the x _
' coordinate (3rd argument), x coordinate (4th argument), width _
' (5th argument), and height (6th argument) of the source rectangle.
e.Graphics.DrawImage( _
   bitmap, _
   New Rectangle(30, 0, iWidth, iHeight), _
   0.0F, _
   0.0F, _
   iWidth, _
   iHeight, _
   GraphicsUnit.Pixel, _
   imageAtt)

// Create the Bitmap object and load it with the texture image.
Bitmap bitmap = new Bitmap("Texture.jpg");

// Initialize the color matrix.
// Note the value 0.8 in row 4, column 4.
float[][] matrixItems ={ 
   new float[] {1, 0, 0, 0, 0},
   new float[] {0, 1, 0, 0, 0},
   new float[] {0, 0, 1, 0, 0},
   new float[] {0, 0, 0, 0.8f, 0}, 
   new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

// Create an ImageAttributes object and set its color matrix.
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(
   colorMatrix,
   ColorMatrixFlag.Default,
   ColorAdjustType.Bitmap);

// First draw a wide black line.
e.Graphics.DrawLine(
   new Pen(Color.Black, 25),
   new Point(10, 35),
   new Point(200, 35));

// Now draw the semitransparent bitmap image.
int iWidth = bitmap.Width;
int iHeight = bitmap.Height;
e.Graphics.DrawImage(
   bitmap,
   new Rectangle(30, 0, iWidth, iHeight),  // destination rectangle
   0.0f,                          // source rectangle x 
   0.0f,                          // source rectangle y
   iWidth,                        // source rectangle width
   iHeight,                       // source rectangle height
   GraphicsUnit.Pixel,
   imageAtt);

Kompilieren des Codes

Das vorhergehende Beispiel ist für die Verwendung mit Windows Forms konzipiert und erfordert PaintEventArgse, einen Parameter von PaintEventHandler.

Siehe auch

Weitere Ressourcen

Grafik und Zeichnen in Windows Forms

Alphablending von Linien und Füllungen