41

I am making an application in C# .NET. I have 8 picture boxes in it. I used PNG images with transparent background but in my form it is not transparent when it comes above another image.

I am using Visual Studio 2012. This is a screenshot of my form:

Screenshot of Form

Termininja
  • 6,620
  • 12
  • 48
  • 49
Belal Khan
  • 2,099
  • 2
  • 22
  • 32
  • 1
    Take a look at http://stackoverflow.com/questions/5522337/c-sharp-picturebox-transparent-background-doesnt-seem-to-work – Binke Nov 11 '13 at 15:46
  • 1
    possible duplicate of [Make overlapping picturebox transparent in C#.net](http://stackoverflow.com/questions/4623165/make-overlapping-picturebox-transparent-in-c-net) – mbeckish Nov 11 '13 at 15:47
  • There is a solution using a normal control like Panel, ... We can draw the image ourselves, however it's hard for a `PictureBox`. Even the solution for another control is not really good if you want to move the object at runtime (because of flicker). – King King Nov 11 '13 at 19:01
  • I know this is an old question, but why don't you have once single picturebox and then get the Graphics object and use the graphics.DrawImage to draw all the images to the picturebox image, instead of having a picturebox object on the form for every image you have to draw and move around? – nivs1978 Oct 17 '16 at 11:40

7 Answers7

77

One way to do this is by changing the parent of the overlapping picture box to the PictureBox over which it is lapping. Since the Visual Studio designer doesn't allow you to add a PictureBox to a PictureBox, this will have to be done in your code (Form1.cs) and within the Intializing function:

public Form1()
{
    InitializeComponent();
    pictureBox7.Controls.Add(pictureBox8);
    pictureBox8.Location = new Point(0, 0);
    pictureBox8.BackColor = Color.Transparent;
}

Just change the picture box names to what ever you need. This should return:

enter image description here

Erik Humphrey
  • 345
  • 5
  • 18
SuperPrograman
  • 1,814
  • 17
  • 24
  • I am using two pictures that are of different dimensions. When I set one image to be the parent of the other only the overlapping portion gets displayed, and anything that's outside the parent's boundaries gets cut off. Any solution to this? – valsidalv Sep 16 '14 at 16:11
  • 2
    @valsidalv I'm sorry, I haven't been in Visual Studio for a bit, but from what I remember, wouldn't it be possible just to enlarge the parent image (without stretching, etc...), so that it would also display all of the child? – SuperPrograman Sep 18 '14 at 14:32
  • I thought so too... I ended up following this tutorial and creating a custom control containing my images http://www.codeproject.com/Articles/25048/How-to-Use-Transparent-Images-and-Labels-in-Window – valsidalv Sep 18 '14 at 17:42
7

GameBoard is control of type DataGridView; The image should be type of PNG with transparent alpha channel background;

        Image test = Properties.Resources.checker_black;
        PictureBox b = new PictureBox();
        b.Parent = GameBoard;
        b.Image = test;
        b.Width = test.Width*2;
        b.Height = test.Height*2;
        b.Location = new Point(0, 90);
        b.BackColor = Color.Transparent;
        b.BringToFront();

enter image description here

Stav Bodik
  • 2,018
  • 3
  • 18
  • 25
2

Try using an ImageList

ImageList imgList = new ImageList;

imgList.TransparentColor = Color.White;

Load the image like this:

picturebox.Image = imgList.Images[img_index];
tttony
  • 4,944
  • 4
  • 26
  • 41
1

I've had a similar problem like this. You can not make Transparent picturebox easily such as picture that shown at top of this page, because .NET Framework and VS .NET objects are created by INHERITANCE! (Use Parent Property).

I solved this problem by RectangleShape and with the below code I removed background, if difference between PictureBox and RectangleShape is not important and doesn't matter, you can use RectangleShape easily.

private void CreateBox(int X, int Y, int ObjectType)
{
    ShapeContainer canvas = new ShapeContainer();
    RectangleShape box = new RectangleShape();
    box.Parent = canvas;
    box.Size = new System.Drawing.Size(100, 90);
    box.Location = new System.Drawing.Point(X, Y);
    box.Name = "Box" + ObjectType.ToString();
    box.BackColor = Color.Transparent;
    box.BorderColor = Color.Transparent;
    box.BackgroundImage = img.Images[ObjectType];// Load from imageBox Or any resource
    box.BackgroundImageLayout = ImageLayout.Stretch;
    box.BorderWidth = 0;
    canvas.Controls.Add(box);   // For feature use 
}
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 1
    Trying to do this, however, I can't get the RectangleShape to appear on top of the other controls (i.e. it seems to be below them and so doesn't appear on the form) – komodosp Jul 09 '15 at 14:53
1

One fast solution is set image property for image1 and set backgroundimage property to imag2, the only inconvenience is that you have the two images inside the picture box, but you can change background properties to tile, streched, etc. Make sure that backcolor be transparent. Hope this helps

i31nGo
  • 1,422
  • 15
  • 11
1

Just use the Form Paint method and draw every Picturebox on it, it allows transparency :

    private void frmGame_Paint(object sender, PaintEventArgs e)
    {
        DoubleBuffered = true;
        for (int i = 0; i < Controls.Count; i++)
            if (Controls[i].GetType() == typeof(PictureBox))
            {
                var p = Controls[i] as PictureBox;
                p.Visible = false;
                e.Graphics.DrawImage(p.Image, p.Left, p.Top, p.Width, p.Height);
            }
    }
roulioz
  • 19
  • 1
-1

you can set the PictureBox BackColor proprty to Transparent

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • 3
    Yeah but that works only for form background. I did it but as you can see in the screenshot when the pictureboxes overlaps it doesn't work. – Belal Khan Nov 11 '13 at 15:48