如何:使用颜色矩阵在图像中设置 Alpha 值

Bitmap (继承自 Image 类)和 ImageAttributes 类提供获取和设置像素值的功能。 可以使用ImageAttributes类来修改整个图像的 SetPixel alpha 值,也可以调用Bitmap类的方法来修改单个像素值。

示例:

ImageAttributes 类具有许多可用于在呈现期间修改图像的属性。 在下面的示例中, ImageAttributes 对象用于将所有 alpha 值设置为 80% 的字母值。 这是通过初始化颜色矩阵并将矩阵中的 alpha 缩放值设置为 0.8 来完成的。 颜色矩阵的地址传递给 SetColorMatrix 对象的方法 ImageAttributes ,并将 ImageAttributes 对象传递给 DrawString 对象的方法 Graphics

在渲染过程中,位图中的 alpha 值会被转换为其原来值的 80%。 这会导致与背景混合的图像。 如下图所示,位图图像看起来透明;你可以看到纯黑色线穿过它。

使用矩阵进行 alpha 混合的图像截图。

当图像位于背景的白色部分时,图像已与白色融合。 图像在越过黑线的位置,会与黑色相融合。

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

编译代码

前面的示例专用于 Windows 窗体,它需要 PaintEventArgse,这是 PaintEventHandler 的参数。

另请参阅