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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Khalad
  • 73
  • 1
  • 1
  • 5
  • What exactly is under the picturebox that you want to be visible? – Cody Gray - on strike Feb 13 '11 at 11:18
  • try this instead: http://stackoverflow.com/questions/4777203/animation-effects-in-winforms-c/4777987#4777987 – Daniel Mošmondor Feb 13 '11 at 11:46
  • This question gets asked a lot. See: [C#, how to make a picture background transparent?](http://stackoverflow.com/questions/4416934/c-how-to-make-a-picture-background-transparent) and [A PictureBox Problem](http://stackoverflow.com/questions/4144371/a-picturebox-problem), among many *many* others. – Cody Gray - on strike Feb 13 '11 at 14:12

3 Answers3

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.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 8
    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. – CodesInChaos Feb 13 '11 at 11:24
  • @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. – Sergey Berezovskiy Feb 13 '11 at 11:53
  • 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. – Grant Thomas Feb 13 '11 at 11:55
  • @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. – Sergey Berezovskiy Feb 13 '11 at 12:18
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.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • In winforms Transparent won't work for Form background, but it works fine for controls. – Sergey Berezovskiy Feb 13 '11 at 11:15
  • And there is no PictureBox control in WPF. There is an Image control. – Sergey Berezovskiy Feb 13 '11 at 11:17
  • @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. – Grant Thomas Feb 13 '11 at 11:24
  • Label works fine. There are controls which do not allow transparent background (ListBox, TextBox, etc) but this is another story. – Sergey Berezovskiy Feb 13 '11 at 11:27
  • 1
    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. – Grant Thomas Feb 13 '11 at 11:29
  • @Mr. Disappointment: Here you are http://www.speedyshare.com/files/26839573/WindowsApplication5.zip – Sergey Berezovskiy Feb 13 '11 at 11:37
  • 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 Feb 13 '11 at 11:47
  • 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. http://www.speedyshare.com/files/26839879/WindowsApplication5.zip – Grant Thomas Feb 13 '11 at 11:48
  • 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. http://support.microsoft.com/kb/943454). But in my life default 'transparency' always was enough to use. – Sergey Berezovskiy Feb 13 '11 at 12:01
  • 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. – Grant Thomas Feb 13 '11 at 12:05
  • 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) – Sergey Berezovskiy Feb 13 '11 at 12:11
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 ;)

Don
  • 1