[英]C# Graphics.Clear(Color.Transparent) not working properly?
I'm drawing some graphics on a control area repeatedly, and each time I want to start fresh with a transparent background.我在控制区域反复绘制一些图形,每次我想从透明背景重新开始。 So I use:所以我使用:
Graphics graph = control.CreateGraphics();
graph.Clear(Color.Transparent);
However, the graphics area seems to turn black instead of transparent.但是,图形区域似乎变成黑色而不是透明。 Any ideas?有任何想法吗?
I found an alternate way to achieve what I need.我找到了另一种方法来实现我所需要的。 I create a new transparent bitmap with the control's dimensions, draw my stuff on it and then use it as a background image for the control:我使用控件的尺寸创建了一个新的透明位图,在其上绘制我的东西,然后将其用作控件的背景图像:
Bitmap bitmap = new Bitmap(control.Width, control.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bitmap.MakeTransparent();
Graphics graph = Graphics.FromImage(bitmap);
// draw stuff here ...
control.BackgroundImage = bitmap;
graph.Dispose();
This works perfectly.这完美地工作。
because picturebox has not transparency layer.因为图片框没有透明层。 you could be use transfer bitmap.你可以使用传输位图。 try this.尝试这个。
tmp_bmp = new Bitmap(picturebox1.Width, picturebox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics Canvas = Graphics.FromImage(tmp_bmp);
picturebox1.Image = tmp_Type;
Type_Canvas.Clear(Color.Transparent);
This approach worked fine for me:这种方法对我来说很好:
private void MyCustomControl_Paint(object sender, PaintEventArgs e)
{
// Clear background - also works when backcolor is transparent
InvokePaintBackground(this, e);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.