Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 208
Inverted Llama Avatar asked Mar 11 '12 08:03

Inverted Llama


People also ask

Why does my transparent PNG have a black background?

The default background is black, so your transparency will look black unless there is anything behind it or you change the background. The PNG is absolutely transparent.

Why do transparent images turn black in PDF?

Information. When you create a PDF from the Outlook NetDocuments toolbar (in an active New message/Reply/Forward with an attachment) using the 'Edit' button the output PDF may change the display of images with transparency to reverse colors like a negative image. e.g. black becomes white and white becomes black.

How do I remove the black background from a PNG image?

When a PNG image with a transparent background is selected from the Recent FIle selector and appears with a black background, the black background can be removed by re-uploading the image as a New File each time you use the image.


3 Answers

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

like image 101
Jmyster Avatar answered Nov 14 '22 23:11

Jmyster


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.

like image 27
Andrii Diachenko Avatar answered Nov 15 '22 00:11

Andrii Diachenko


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

like image 28
Pedery Avatar answered Nov 14 '22 23:11

Pedery