Comment : utiliser une matrice de couleurs pour définir des valeurs alpha dans des images

Mise à jour : novembre 2007

La classe Bitmap (qui hérite de la classe Image) et la classe ImageAttributes fournissent les fonctionnalités d'obtention et de définition de valeurs de pixels. Vous pouvez utiliser la classe ImageAttributes pour modifier les valeurs alpha de toute une image ou vous pouvez appeler la méthode SetPixel de la classe Bitmap pour modifier des valeurs de pixel individuelles.

Exemple

La classe ImageAttributes comporte plusieurs propriétés que vous pouvez utiliser pour modifier les images pendant le rendu. Dans l'exemple suivant, un objet ImageAttributes est employé pour définir toutes les valeurs alpha à 80 pour cent de la valeur d'origine. Cela est effectué en initialisant une matrice de couleurs et en définissant la valeur de dimensionnement alpha dans la matrice à 0,8. L'adresse de la matrice couleur est passée à la méthode SetColorMatrix de l'objet ImageAttributes et l'objet ImageAttributes est passé à la méthode DrawString de l'objet Graphics.

Pendant le rendu, les valeurs alpha dans la bitmap sont converties à 80 pour cent de leur valeur d'origine. Cela donne une image fondue à l'arrière-plan. Comme le montre l'illustration suivante, l'image bitmap semble transparente ; vous pouvez voir la ligne noire unie à travers.

Fusion alpha utilisant une matrice

Là où l'image se trouve au-dessus de la partie blanche de l'arrière-plan, l'image a été fondue à la couleur blanche. Là où l'image traverse la ligne noire, l'image se fond à la couleur noire.

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

Compilation du code

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

Voir aussi

Autres ressources

Graphiques et dessins dans les Windows Forms

Fusion alpha de lignes et de remplissages