在picturebox的透明背景 [英] Transparent background on picturebox

查看:79
本文介绍了在picturebox的透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让图片框的背景透明。在图片框(矩形)中,我放置了一个图标(圆形)。我使图标透明,以便可以看到图标下方的其他药水。我尝试设置.backcolor = Transparent,但它不起作用。此外,在运行时使用.fromargb它不起作用。你对这个问题有什么解决方案吗?



提前谢谢

I want to make the background of a picturebox transparent. In the picturebox (rectangular shape), I placed an icons (circular in shape). I make the icon transparent so that other potion underneath the icon can be visible. I tried with setting the .backcolor = Transparent, but it doesn't work. Also during runtime with the .fromargb it doesn't work. Do you have any solution of this problem?

Thanks in advance

推荐答案

透明,就像你一样我们发现了,不是。什么透明告诉控件它应该使用包含你正在制作透明的控件的控件的背景属性。



例如,通过设置BackColor PictureBox to Transparent你告诉PictureBox采用包含它的控件的背景属性。在您的情况下,这可能是您放置PictureBox的表单。当PictureBox具有与容器相同的背景属性时,即使您无法看透它,也会产生透明控件的错觉。



Windows窗体控件从不透明。好吧,至少不是没有跳过很​​多箍来自定义渲染控件以使其透明。
Transparent, as you've found out, isn't. What Transparent tells the control is it should use the background properties of the control that contains the control you're making "transparent".

For example, by setting the BackColor of the PictureBox to Transparent you're telling the PictureBox to take on the background properties of the control that contains it. In your case, that's probably the Form you dropped the PictureBox on. When the PictureBox has the same background properties of the container it's in this gives the illusion of a transparent control even though you can't see through it.

Windows Forms controls are never transparent. Well, at least not without jumping through a lot of hoops to custom render the control so that it is transparent.


我建​​议:不要使用PictureBox。

直接在Form Paint事件中绘制图像。
I would suggest: don't use a PictureBox.
Draw the image directly yourself in the Form Paint event.


我知道这是一个非常老的线程 - 但是根据Dave的建议你可以看到一个简单的控制修改使(自定义)PictureBox真正透明。



I know that this is a very old Thread - but following the Suggestion of Dave you can see a simple control-modification which makes a (customized) PictureBox real transparent.

public class Test_PictureBox : System.Windows.Forms.PictureBox
{

    public Test_PictureBox()
    {
        this.Width = 100;
        this.Height = 100;

        SetTransparenz();
    }

    private void SetTransparenz()
    {
        this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
        this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, false);
        this.SetStyle(System.Windows.Forms.ControlStyles.ResizeRedraw, true);
    }

    protected override System.Windows.Forms.CreateParams CreateParams
    {
        get
        {
            System.Windows.Forms.CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | 0x20;
            // Turn on WS_EX_TRANSPARENT
            return cp;
        }
    }

}


这篇关于在picturebox的透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆