如何:使用色彩矩陣設定影像中的 Alpha 值
類別 Bitmap (繼承自 Image 類別),而 類別 ImageAttributes 提供取得和設定圖元值的功能。 您可以使用 類別 ImageAttributes 來修改整個影像的 Alpha 值,也可以呼叫 SetPixel 類別的 Bitmap 方法來修改個別圖元值。
範例
類別 ImageAttributes 有許多屬性,您可以在轉譯期間用來修改影像。 在下列範例中, ImageAttributes 物件用來將所有 Alpha 值設定為 80% 的 Alpha 值。 這是藉由初始化色彩矩陣,並將矩陣中的 Alpha 縮放值設定為 0.8 來完成。 色彩矩陣的位址會傳遞至 SetColorMatrix 物件的 方法 ImageAttributes ,而 ImageAttributes 物件會傳遞至 DrawString 物件的 方法 Graphics 。
在轉譯期間,點陣圖中的 Alpha 值會轉換成其 80%。 這會產生與背景混合的影像。 如下圖所示,點陣圖影像看起來是透明的;您可以看到實心黑色線條。
影像位於背景的白色部分,影像已與色彩白色混合。 影像交叉黑線的位置,影像會與黑色混合。
// 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 Form 使用所設計,而且需要 PaintEventArgse
,這是 PaintEventHandler 的參數。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應