Can I create a transparent background on a PictureBox in WinForms?

15,830

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.

Share:
15,830

Related videos on Youtube

Khalad
Author by

Khalad

Updated on June 04, 2022

Comments

  • Khalad
    Khalad almost 2 years

    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?

  • Sergey Berezovskiy
    Sergey Berezovskiy about 13 years
    In winforms Transparent won't work for Form background, but it works fine for controls.
  • Sergey Berezovskiy
    Sergey Berezovskiy about 13 years
    And there is no PictureBox control in WPF. There is an Image control.
  • Grant Thomas
    Grant Thomas about 13 years
    @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.
  • Sergey Berezovskiy
    Sergey Berezovskiy about 13 years
    Label works fine. There are controls which do not allow transparent background (ListBox, TextBox, etc) but this is another story.
  • Grant Thomas
    Grant Thomas about 13 years
    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.
  • Sergey Berezovskiy
    Sergey Berezovskiy about 13 years
    @Mr. Disappointment: Here you are speedyshare.com/files/26839573/WindowsApplication5.zip
  • Grant Thomas
    Grant Thomas about 13 years
    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?
  • Grant Thomas
    Grant Thomas about 13 years
    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.zip
  • Sergey Berezovskiy
    Sergey Berezovskiy about 13 years
    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.
  • Grant Thomas
    Grant Thomas about 13 years
    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.
  • Sergey Berezovskiy
    Sergey Berezovskiy about 13 years
    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)