如何:绘制具有透明度的图像

更新: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 对象将由垃圾回收器销毁,不需要显式释放。

请参见

其他资源

.NET Compact Framework 中的图形和绘图