Click here to Skip to main content
15,116,813 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted
Updated 29-Feb-16 22:34pm

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.
   
Comments
Ralf Meier 29-Feb-16 6:48am
   
I don't agree with you Dave.
It isn't a lot of work to make a Forms-Control really transparent ...
Dave Kreskowiak 29-Feb-16 8:36am
   
Funny. I didn't see you put an answer on this thread. If you want to walk him through the process of doing it and support the code you've put up in any situation, feel free.
Ralf Meier 1-Mar-16 3:36am
   
As you suggested I have done it.
Please refer to Solution 6 ...
Richard Deeming 1-Apr-16 14:58pm
   
Did you notice this question is from 2011?
Dave Kreskowiak 2-Apr-16 9:27am
   
Yeah. I noticed about 20 minutes after I posted this.
I would suggest: don't use a PictureBox.
Draw the image directly yourself in the Form Paint event.
   
Comments
Sandeep Mewara 13-Feb-11 6:17am
   
Comment from OP:
Thanks for your reply. But is it possible to do the same thing using picturebox?
OriginalGriff 13-Feb-11 6:21am
   
Not without a lot of work: and in practice it ends up as the same thing. Do it in the paint event: picture boxes are a bit too simplistic for what you are trying to do.
Manfred Rudolf Bihy 13-Feb-11 7:20am
   
Proposed as answer! 5+
Sergey Alexandrovich Kryukov 13-Feb-11 12:45pm
   
Excellent, my 5.
One should note: it does not resolve the problem, it eliminates it.
--SA
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.

C#
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;
        }
    }

}
   
v2
If you really want a circular effect why not override the PictureBox to behave as a Circular Picture Box. I found the code somewhere in the internet, Feel free to use it...

C#
class OvalPictureBox : PictureBox
{
    public OvalPictureBox()
    {
        this.BackColor = Color.Transparent;
    }
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        using (var gp = new GraphicsPath())
        {
            gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            this.Region = new Region(gp);
        }
    }
}
   
Comments
CHill60 28-Feb-16 10:02am
   
If you "found the code somewhere on the internet" then it is not up to you to say "Feel free to use it" unless the _original_ author said it was ok!
Ozesh Thapa 8-Mar-16 2:14am
   
It was an answer provided by an author to a similar question a while ago.
(I would have redirected the answer to that link), i just had the code, so i passed it on.
"Feel free to use it" is ok here, because, you would write the same code yourself to create a Circular PictureBox. So why waste time and write it again, if you can freely create a class and paste the above code, Hence "Feel free to use it".
The world would have been a better place if you had not wasted both of our time in unwanted discussions.
Peace !!
CHill60 8-Mar-16 4:41am
   
The world would have been a better place if you had just stuck to explaining about the code! I don't consider time spent warning people about the dangers of plagiarism as time wasted.
Richard Deeming 1-Apr-16 14:58pm
   
This question was asked, and solved, five years ago!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900