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

本文介绍了一种在.NET Compact Framework中处理位图透明性的方法,包括通过设置特定颜色作为透明键来实现图像部分透明的技术。文章提供了使用Visual Basic和C#的示例代码,展示了如何创建带有透明背景的红色和黑色矩形位图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例

此示例使用红色和黑色设计方案创建矩形的 Bitmap,并演示设置透明的两种技术:

  • 使用基于图像中的一个像素的 SetColorKey 方法。此示例使用图像左上角的像素设置透明。由于此像素为黑色,因此所有最初为黑色的像素都将是透明的。

  • SetColorKey 方法用于显式颜色设置。此示例将它设置为红色,因此所有最初为红色的像素都将是透明的。

若要进行演示,请首先注释掉这两种透明技术来运行应用程序,以查看未进行透明设置的图像如何显示。然后取消对任意一种透明技术的代码的注释。

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

 把图片边黑白

void whiteblack(Bitmap bmp)
{
for(int a=0;a<bmp.Width;a++)
{
for(int b=0;b<bmp.Height;b++)
{
int r=bmp.GetPixel(a,b).R;
int g=bmp.GetPixel(a,b).G;
int bb=bmp.GetPixel(a,b).B;
Color c;
int v=(r+g+bb)/3;
if(v>255/2)c=Color.FromArgb(255,255,255);
else
c=Color.FromArgb(0,0,0);
bmp.SetPixel(a,b,c);
}

变灰色

void grey(Bitmap bmp)
{
for(int a=0;a<bmp.Width;a++)
{
for(int b=0;b<bmp.Height;b++)
{
int r=bmp.GetPixel(a,b).R;
int g=bmp.GetPixel(a,b).G;
int bb=bmp.GetPixel(a,b).B;
Color c;
int v= (int)(0.299 *r+ 0.587 * g + 0.114 * b)  //(r * 77 + g * 151 + b * 28) >> 8

c=Color.FromArgb(v,v,v);
bmp.SetPixel(a,b,c);
}
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值