HOW TO:以透明度繪製影像
更新:2007 年 11 月
.NET Compact Framework 支援透明度,但只有一種透明度色彩。SetColorKey(Color, Color) 方法必須針對低色彩和高色彩範圍指定相同的色彩。
範例
此範例會以紅色和黑色的設計來建立矩形的 Bitmap,並示範兩種設定透明度的技巧:
根據影像的像素來使用 SetColorKey(Color, Color) 方法。此範例會使用影像左上方的像素來設定透明度。因為此像素是黑色的,所有原先為黑色的像素將會變成透明。
以明確的色彩設定來使用 SetColorKey(Color, Color) 方法。此範例會將色彩設為紅色,因此所有原先為紅色的像素將會變成透明。
若要示範,先將兩種透明度技巧標記為註解再執行應用程式,以查看影像沒有設定透明度時出現的樣子。然後針對其中一種技巧,取消註解程式碼。
' The .NET Compact Framework supports transparency,
' but with only one transparency color.
' The SetColorKey method must have the same color
' specified for the low color and high color range.
' Note that you must uncomment lines of code
' as indicated for the desired transparency effect.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' Create a red and black bitmap to demonstrate transparency.
Dim bmp As New Bitmap(75, 75)
Dim g As Graphics = Graphics.FromImage(bmp)
g.FillEllipse(New SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width)
g.DrawLine(New Pen(Color.Black), 0, 0, bmp.Width, bmp.Width)
g.DrawLine(New Pen(Color.Black), bmp.Width, 0, 0, bmp.Width)
g.Dispose()
Dim attr As New ImageAttributes
' Set the transparency color key based on the upper-left pixel
' of the image.
' Uncomment the following line to make all black pixels transparent:
' attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0))
' Set the transparency color key based on a specified value.
' Uncomment the following line to make all red pixels transparent:
' attr.SetColorKey(Color.Red, Color.Red)
' Draw the image using the image attributes.
Dim dstRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, _
GraphicsUnit.Pixel, attr)
End Sub
// The .NET Compact Framework supports transparency,
// but with only one transparency color.
// The SetColorKey method must have the same color
// specified for the low color and high color range.
// Note that you must uncomment lines of code
// as indicated for the desired transparency effect.
protected override void OnPaint(PaintEventArgs e)
{
// Create a red and black bitmap to demonstrate transparency.
Bitmap bmp = new Bitmap(75,75);
Graphics g = Graphics.FromImage(bmp);
g.FillEllipse(new SolidBrush(Color.Red), 0, 0, bmp.Width, bmp.Width);
g.DrawLine(new Pen(Color.Black), 0, 0, bmp.Width, bmp.Width);
g.DrawLine(new Pen(Color.Black), bmp.Width, 0, 0, bmp.Width);
g.Dispose();
ImageAttributes attr = new ImageAttributes();
// Set the transparency color key based on the upper-left pixel
// of the image.
// Uncomment the following line to make all black pixels transparent:
// attr.SetColorKey(bmp.GetPixel(0, 0), bmp.GetPixel(0, 0));
// Set the transparency color key based on a specified value.
// Uncomment the following line to make all red pixels transparent:
// attr.SetColorKey(Color.Red, Color.Red);
// Draw the image using the image attributes.
Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
e.Graphics.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height,
GraphicsUnit.Pixel, attr);
}
編譯程式碼
這個範例需要下列命名空間的參考:
穩固程式設計
請注意,用來建立點陣圖的 Graphics 物件會明確地加以處置。由 PaintEventArgs 物件的 Graphics 屬性傳回的 Graphics 物件,可由記憶體回收行程終結,而不需要明確地加以處置。