简体   繁体   中英

Transparent background showing up black

I'm trying to make an image appear on top of another and still show the image underneath via a transparent background. I've got it so the new image appears on top of the other however setting BackColor to Color.Transparent just results in a black background.

Full Code:

public partial class frm_airportApplication : Form
{
    PictureBox PicBox;
    public frm_airportApplication()
    {
        InitializeComponent();
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x000000200;
            return cp;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        AllowTransparency = true;
        plane p = new plane();
        p.getPB().Parent = pb_airport;
        this.Controls.Add(p.getPB());
        this.Update();
    }
    protected void InvalidateEx()
    {
        if (Parent == null)
            return;
        Rectangle rc = new Rectangle(this.Location, this.Size);
        Parent.Invalidate(rc, true);
    }
    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        //do not allow the background to be painted 
    }

    private void button2_Click(object sender, EventArgs e)
    {
        AllowTransparency = true;
        ResourceManager resourceManager = new ResourceManager("Airport_Application.Properties.Resources", GetType().Assembly); 
        PicBox = new PictureBox();
        PicBox.BackColor = Color.Transparent;
        PicBox.Image = (Bitmap)resourceManager.GetObject("plane_icon");
        PicBox.Top = 100;
        PicBox.Width = 120;
        PicBox.Height = 120;
        PicBox.Left = 10;
        PicBox.SizeMode = PictureBoxSizeMode.Zoom;

        PicBox.Parent = pb_airport;
        Controls.Add(PicBox);
        PicBox.BringToFront();
    }
}
public class plane
{
    PictureBox pb;
    Bitmap image;
    ResourceManager resourceManager;
    public plane()
    {
        resourceManager = new ResourceManager("Airport_Application.Properties.Resources", GetType().Assembly);
        image=(Bitmap)resourceManager.GetObject("plane_icon");
        pb = new PictureBox();
        pb.Image = image;
        pb.Top = 500;
        pb.Width = 100;
        pb.Height = 100;
        pb.Left = 50;
        pb.SizeMode = PictureBoxSizeMode.Zoom;
        pb.BackColor = Color.Transparent;
    }
    public PictureBox getPB()
    {
        return pb;
    }
}

I've found a lot of people who have had similar issues but none of the solutions helped.

It has been awhile but I think you have to set your form to Allow Tranparencies

this.AllowTransparency = true;
or
YourForm.AllowTransparency = true;

that would get rid of the black

I had the same issue but I had just a Panel which should've been transparent so I could see everything underneath it.

The problem was with DoubleBuffered property, it should be set to false .

this.DoubleBuffered = false;

No blackness anymore.

You can create an irregularly shaped form easily by setting its "Region" property. Here's an example:

Irregularly shaped form

As for truly transparent Controls, here's an excellent resource with step-by-step instructions:

Transparent Controls

If you want to overlay images over images (and not images over form), this would make the trick:

overImage.Parent = backImage;
overImage.BackColor = Color.Transparent;
overImage.Location = thePointRelativeToTheBackImage;

Where overImage and backImage are PictureBox with png (with transparent background).

For forms you can try this:

        this.BackColor = System.Drawing.Color.XXX;
        this.TransparencyKey = System.Drawing.Color.XXX;

You can try to solve it on the bitmap level:

Make a image in bitmap format and make the backgroundcolor transparant with this method:

        bm.MakeTransparent(Color.XXX); 

I seemed to solve a similar problem with my splashscreen bij setting a timer every 100ms, and call DoEvents in it:

    private void timer1_Tick(object sender, EventArgs e)
    {
        //BringToFront();
        Application.DoEvents();
    }

Hope this helps

In simple words, you cannot easily achieve transparency using the default PictureBox control in Windows Forms.

Either you switch to WPF, which by default supports transparency in every bits, or you use a custom control. Once I created such a control called AppIcon, but it is released under GPL, not commercial friendly,

http://mymobilepack.codeplex.com/SourceControl/changeset/view/39314#512415

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM