Can I create a transparent background on a PictureBox in WinForms?
Asked Answered
S

3

7

I want to make the background of a PictureBox control transparent. In the PictureBox (rectangular shape), I placed an icon (circular in shape). I want to make the icon transparent so that the other portion underneath the icon is visible.

I have tried setting the PictureBox.BackColor property to "Transparent", but it doesn't work. I also tried to set it during runtime with the Color.FromArgb method, but it doesn't work either.

Is there any solution to this problem?

Sejant answered 13/2, 2011 at 11:3 Comment(3)
What exactly is under the picturebox that you want to be visible?Robichaux
try this instead: #4777703Heartland
This question gets asked a lot. See: C#, how to make a picture background transparent? and A PictureBox Problem, among many many others.Robichaux
K
4

Setting pictureBox.BackColor = Color.Transparent; definitely should work.

Also verify if you are setting alpha channel of color when using Color.FromArgb(0, 0, 0, 0); (this is a first parameter, zero means transparent color)

And, of course, make sure your icons have transparent background.

Kermes answered 13/2, 2011 at 11:11 Comment(4)
Setting the background color to transparent doesn't make these pixels transparent by default. It makes them have the background color of the parent control.Apocrine
@CodeInChaos: sure, that's can be seen when 'transparent' control overlaps other control. That's because 'transparent' control asks parent control to draw it's background.Kermes
AFAIK, this will only work as per your example in comments to my answer: that is, when the parent controls don't use a solid color, i.e. an image.Decima
@Mr. Disappointmen: This is not depends on what type of background used by parent control - whether it image or solid control, or userPaint - parent's background will be drawn on 'transparent' child.Kermes
D
2

If using WinForms then Setting the background color to transparent won't work as transparency handling is not a cascading system - you can only (in most cases) set transparency (or rather the opacity) of a control overall using the Opacity property, however this will alter the alpha channel of the entire control display giving your images a see-througness.

One solution might be to set the background color of the PictureBox to be that of the control beneath it (the color of the form, for example). But this may not suffice in your situation.

Decima answered 13/2, 2011 at 11:13 Comment(11)
In winforms Transparent won't work for Form background, but it works fine for controls.Kermes
And there is no PictureBox control in WPF. There is an Image control.Kermes
@lazyberezovsky: Not all controls - try configuring background transparency for a Label, for instance (and evidently, for PictureBox. Granted I used PictureBox interchangeabley for both frameworks, will remove my references wo WPF anyway.Decima
Label works fine. There are controls which do not allow transparent background (ListBox, TextBox, etc) but this is another story.Kermes
You have an example of Label and/or PictureBox working fine in this manner? As a sanity check I just created a project to test and setting Transparent as the background for either simply leaves both controls with a Control (see: Gray) background.Decima
@Mr. Disappointment: Here you are speedyshare.com/files/26839573/WindowsApplication5.zipKermes
Ah, yes, it works if you don't have a solid color set as the background. You have an image set as the background, surely that's a special case?Decima
For instance, if you remove your image and set the form's background color to Red and overlay two labels, you will see the top label's Red background over the one beneath. speedyshare.com/files/26839879/WindowsApplication5.zipDecima
See my comment about 'transparency' in winforms. No matter what background of parent is - image or solid color - parent will draw it on child control. But parent do not know anything about overlaying. So you get this behavior. There are some ways to achieve overlaying transparency (e.g. support.microsoft.com/kb/943454). But in my life default 'transparency' always was enough to use.Kermes
Well, it's pretty flawed; even in your original example, if you move the Label over the little icon PictureBox then you will see the fail.Decima
1) I described the reason of this behavior (because Form is the parent of Label); 2) Question was about transparent pictureBox, not about transparency implementation in Winforms; 3) If you want label over picture - use control, which could be parent to the label (e.g. Panel)Kermes
T
0

hi you must set the icon on the other portion underneath the icon by using this

icon_pictureBox_name.Controls.Add(other_portion_picturBox_name);

and after that you can set the PictureBox.BackColor property to "Transparent" and it will work ;)

Thymic answered 6/4, 2014 at 2:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.