Can I create a transparent background on a PictureBox in WinForms?
Solution 1
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.
Solution 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.
Related videos on Youtube
Khalad
Updated on June 04, 2022Comments
-
Khalad almost 2 years
I want to make the background of a
PictureBox
control transparent. In thePictureBox
(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 theColor.FromArgb
method, but it doesn't work either.Is there any solution to this problem?
-
Sergey Berezovskiy about 13 yearsIn winforms Transparent won't work for Form background, but it works fine for controls.
-
Sergey Berezovskiy about 13 yearsAnd there is no PictureBox control in WPF. There is an Image control.
-
Grant Thomas about 13 years@lazyberezovsky: Not all controls - try configuring background transparency for a
Label
, for instance (and evidently, forPictureBox
. Granted I usedPictureBox
interchangeabley for both frameworks, will remove my references woWPF
anyway. -
Sergey Berezovskiy about 13 yearsLabel works fine. There are controls which do not allow transparent background (ListBox, TextBox, etc) but this is another story.
-
Grant Thomas about 13 yearsYou have an example of
Label
and/orPictureBox
working fine in this manner? As a sanity check I just created a project to test and settingTransparent
as the background for either simply leaves both controls with aControl
(see: Gray) background. -
Sergey Berezovskiy about 13 years@Mr. Disappointment: Here you are speedyshare.com/files/26839573/WindowsApplication5.zip
-
Grant Thomas about 13 yearsAh, 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?
-
Grant Thomas about 13 yearsFor 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'sRed
background over the one beneath. speedyshare.com/files/26839879/WindowsApplication5.zip -
Sergey Berezovskiy about 13 yearsSee 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.
-
Grant Thomas about 13 yearsWell, it's pretty flawed; even in your original example, if you move the
Label
over the little iconPictureBox
then you will see the fail. -
Sergey Berezovskiy about 13 years1) 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)