简体   繁体   English

C# Graphics.Clear(Color.Transparent) 不能正常工作?

[英]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.

相关问题 Graphics.Clear(Color.Transparent)在选项卡控件内绘制模糊的文本 - Graphics.Clear(Color.Transparent) draw blurry text inside tab control 如何设置透明位图图像?设置属性Graphics.clear(color.Transparent)时会显示黑色背景。) - How to Creating Transparent Bitmap Image?It gives black background when i set property Graphics.clear(color.Transparent ).) 为什么即使 Bitmap 设置为 Graphics.Clear(Color.Transparent) 时,我的图像周围也会出现黑色背景 - Why am I getting a black background around my images when resizing even when Bitmap is set to Graphics.Clear(Color.Transparent) Graphics.Clear 给定的图像? C# Winforms - Graphics.Clear a given image? C# Winforms Graphics.DrawImage不适用于透明图像C# - Graphics.DrawImage not working for transparent images C# ColorTranslator给出SystemColors.HotTrack和Color.Transparent的错误结果 - ColorTranslator gives incorrect result for SystemColors.HotTrack and Color.Transparent 清除透明面板的图形C# - Clearing the graphics of a transparent panel C# Windows 窗体控件上的 Graphics.Clear 也会清除控件本身 - 如何防止这种情况? - Graphics.Clear on a Windows Form Control also clears the control itself - how to prevent this? 如何在C#中清除面板的图形? - How to clear panel's graphics in c#? 无法使用 Color.FromArgb 设置透明度(但 Color.Transparent 有效) - Unable to set transparency using Color.FromArgb (but Color.Transparent works)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM