CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2005
    Posts
    63

    Transparent Picture box in Windows Form?

    How do I create a transparent PictureBox in a Windows Form? I tried setting the BackColor to Transparent, but that wasn't valid and didn't work.

  2. #2
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Re: Transparent Picture box in Windows Form?

    If u want fully-transparent just turn the visibility property 2 false

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Transparent Picture box in Windows Form?

    i suggest you use a panel loaded with a background picture instead - picturebox cant receive some events either (DragDrop )
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Transparent Picture box in Windows Form?

    I'm not sure that it's possible, some controls in .NET do not behave as expected.

    You can always try using a Panel instead. Either set its BackgroundImage property, or subscribe to the Paint event and display your image.
    [Edit. cjard beat me to it]
    Useful? Then click on (Rate This Post) at the top of this post.

  5. #5
    Join Date
    Nov 2005
    Posts
    63

    Re: Transparent Picture box in Windows Form?

    So there's no way at all to do this? I changed the image to a Panel and drew it in OnPaint...but the blank spots are still being filled in with some color instead of showing what's behind the image. It would seem like this is common enough that there would be some way to do it.

  6. #6
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Transparent Picture box in Windows Form?

    Did you set the BackColor to Transparent?
    Useful? Then click on (Rate This Post) at the top of this post.

  7. #7
    Join Date
    Nov 2005
    Posts
    63

    Re: Transparent Picture box in Windows Form?

    Yep...still draws yellow'ish pixels instead of transparent (i.e. whatever color the object behind them is) ones.

  8. #8
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Transparent Picture box in Windows Form?

    Instead of setting the BackColor of the picturebox to Transparent, try changing the bitmap itself.
    Code:
    Bitmap pic = new Bitmap(pictureBox1.Image);
    for (int x=0; x<pic.Width; x++)
       for (int y=0; y<pic.Height; y++)
       {
           Color pixel = pic.GetPixel(x,y);
           if (pixel == someColor) //You might need to compare RGB
               pic.SetPixel(x,y,Color.Transparent);
       }
    pictureBox1.Image = pic;
    Put the code in the ImageChanged event of the PictureBox. It will work if you are not changing the picture all the time.

  9. #9
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Transparent Picture box in Windows Form?

    Here is an article on transparency in Windows Forms: http://www.bobpowell.net/transcontrols.htm. I wouldn't recommend the solution he provides but at least it outlines the general limitations.
    Useful? Then click on (Rate This Post) at the top of this post.

  10. #10
    Join Date
    Nov 2005
    Posts
    63

    Re: Transparent Picture box in Windows Form?

    Quote Originally Posted by jhammer
    Instead of setting the BackColor of the picturebox to Transparent, try changing the bitmap itself.
    Thanks!

    I have a map of strings->Images that I load at startup. This map contains all the files that will be in the picturebox. Also, they're all the same shape, so this really makes the most sense as I know exactly what pixels need to be set to Transparent. Works great!

  11. #11
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Transparent Picture box in Windows Form?

    maybe i misunderstood your question.. i thought you were talking about a transparent PictureBox, but i now realise you are talking about loading a picture that contains transparent pixels, into a picturebox and they arent rendered as transparent.. ?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  12. #12
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Transparent Picture box in Windows Form?

    Quote Originally Posted by cjard
    maybe i misunderstood your question.. i thought you were talking about a transparent PictureBox
    Quote Originally Posted by LooselyBased
    How do I create a transparent PictureBox in a Windows Form?
    That may have been the problem!

    Congratulations must go to jhammer for working out the real problem.
    Useful? Then click on (Rate This Post) at the top of this post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured