Results 1 to 6 of 6

Thread: Why isn’t my PictureBox background transparent?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    Question Why isn’t my PictureBox background transparent?

    I have a VB.net forms application. The majority of the form is a DataGridView with the cells having a variety of background colors. In the application, when a button is clicked, a PictureBox is created and it is filled with a PNG image. The image is a circle – black outline with a white interior and a transparent background.

    Following is the code used to place the picture box:
    Code:
    Dim xpos As Integer = <some number>
    Dim yPos As Integer = <some number>
    Dim pba as PictureBox = New PictureBox
    pba.BackColor = Color.Transparent
    pba.Height = 70
    pba.Width = 70
    pba.Location = New Point(xPos, yPos)
    pba.Image = My.Resources.circle
    Controls.Add(pba)
    pba.BringToFront()
    All of this works fine, but the area outside the circle and inside the PictureBox is white, not transparent. I have searched, but have found nothing to address this issue. Did I miss something obvious? Please help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why isn’t my PictureBox background transparent?

    There is no real transparency in Windows Forms. When you make a control transparent, what actually happens is that the control copies the area of its parent that it is covering and paints that in its background. Because your form is the parent of the PictureBox, what you're seeing is the form painted in its background. You may have to use GDI+ to draw the image over the DataGridView instead, although that may be complex because you may have to draw in each cell individually. I'm not sure that you can just draw over the top of the grid as a whole.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Why isn’t my PictureBox background transparent?

    Quote Originally Posted by jmcilhinney View Post
    ...I'm not sure that you can just draw over the top of the grid as a whole.
    You can draw over the dgv. Just handle the Paint event, and use e.Graphics

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why isn’t my PictureBox background transparent?

    Quote Originally Posted by .paul. View Post
    You can draw over the dgv. Just handle the Paint event, and use e.Graphics
    Just tested and can confirm. The one exception is when editing a cell, the editing control will be displayed over the grid and so will obscure the drawing. If that's an issue, you'd have to handle the Paint event of the editing control too, although I'm not sure that a TextBox supports drawing over it anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Why isn’t my PictureBox background transparent?

    I made a game in the past with transparent token in picturebox who move over an other picturebox. I used PNG files who allow transparency background

    here is my test project for the movement if it can help : (project is to large for the local upload so here is a link )

    https://www.swisstransfer.com/d/b280...1-2c490c425219
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Why isn’t my PictureBox background transparent?

    I’ve written a few games that use a dgv with graphics drawn on it.
    Have a look at my compiled games, and guess which ones use a dgv…

Tags for this Thread

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