示例
此示例使用红色和黑色设计方案创建矩形的 Bitmap,并演示设置透明的两种技术:
-
使用基于图像中的一个像素的 SetColorKey 方法。此示例使用图像左上角的像素设置透明。由于此像素为黑色,因此所有最初为黑色的像素都将是透明的。
-
将 SetColorKey 方法用于显式颜色设置。此示例将它设置为红色,因此所有最初为红色的像素都将是透明的。
若要进行演示,请首先注释掉这两种透明技术来运行应用程序,以查看未进行透明设置的图像如何显示。然后取消对任意一种透明技术的代码的注释。
' 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);
}
}
}
}
}