How to make picturebox transparent?
Asked Answered
V

7

43

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

Vanpelt answered 11/11, 2013 at 15:39 Comment(4)
Take a look at #5522837Fibroin
possible duplicate of Make overlapping picturebox transparent in C#.netLegionary
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).Shaunna
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?Unmannered
I
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

Immunity answered 18/11, 2013 at 23:59 Comment(3)
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?Torchbearer
@Torchbearer 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?Immunity
I thought so too... I ended up following this tutorial and creating a custom control containing my images codeproject.com/Articles/25048/…Torchbearer
L
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

Lynettalynette answered 8/6, 2016 at 16:39 Comment(0)
P
2

Try using an ImageList

ImageList imgList = new ImageList;

imgList.TransparentColor = Color.White;

Load the image like this:

picturebox.Image = imgList.Images[img_index];
Preliminary answered 11/11, 2013 at 15:56 Comment(0)
C
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 
}
Crocoite answered 9/2, 2014 at 16:23 Comment(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)Bradberry
I
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

Insupportable answered 18/9, 2014 at 10:18 Comment(0)
A
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);
            }
    }
Axe answered 29/1, 2021 at 14:20 Comment(0)
W
-1

you can set the PictureBox BackColor proprty to Transparent

Wrac answered 11/11, 2013 at 15:45 Comment(1)
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.Vanpelt

© 2022 - 2024 — McMap. All rights reserved.