Results 1 to 10 of 10

Thread: How to make pictures transparent on Visual Basic?

  1. #1

    Thread Starter
    Junior Member usernameplease's Avatar
    Join Date
    Nov 2014
    Posts
    18

    How to make pictures transparent on Visual Basic?

    I am trying to put transparent PNG images on my forms but they seem to be showing white spaces. I also went to the properties window to set each image to transparent but no success. How can I approach this issue?

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How to make pictures transparent on Visual Basic?

    Forms controls only do transparency by showing you the Backcolor (and BackgroundImage) of the Parent control. So if you put a PictureBox on a Form, set its BackColor to Transparent and its Image to a transparent PNG, you will see the Form's background in the transparent areas. It sounds like you want something else to show. Could you please give more detail: what do you want to show in the transparent areas?

    BB

  3. #3

    Thread Starter
    Junior Member usernameplease's Avatar
    Join Date
    Nov 2014
    Posts
    18

    Re: How to make pictures transparent on Visual Basic?

    Quote Originally Posted by boops boops View Post
    Forms controls only do transparency by showing you the Backcolor (and BackgroundImage) of the Parent control. So if you put a PictureBox on a Form, set its BackColor to Transparent and its Image to a transparent PNG, you will see the Form's background in the transparent areas. It sounds like you want something else to show. Could you please give more detail: what do you want to show in the transparent areas?

    BB
    I am trying to put my pictures in buttons actually but they show the whitespaces in the buttons

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How to make pictures transparent on Visual Basic?

    I have no problem showing a transparent PNG on a Button by setting the button's Image property. Maybe the "transparent" areas of your PNGs aren't really transparent after all. Did you save it as a 32-bit image?

    If the background is actually white, you could make all the white pixels transparent like this:
    Code:
    Dim bmp As New Bitmap(filename)
    bmp.MakeTransparent(Color.White)
    Button1.Image=bmp
    MakeTransparent will only work for the exact specified color (White must be [255, 255, 255] and not [254, 255, 255]).

  5. #5
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: How to make pictures transparent on Visual Basic?

    Quote Originally Posted by boops boops View Post
    Forms controls only do transparency by showing you the Backcolor (and BackgroundImage) of the Parent control. So if you put a PictureBox on a Form, set its BackColor to Transparent and its Image to a transparent PNG, you will see the Form's background in the transparent areas.
    BB
    Hi BB,
    I see this was 2015, but how is it done in VS2019 (I've stopped using VS2022 until they fix the resources issue)
    In my VS2019, there is no option for PictureBox BackColor transparent


    Poppa

    PS

    I meant to mention...
    Code:
     PictureBox1.BackColor = Color.Transparent
    ...doesn't throw any errors, but neither does it work.

    Pop
    Last edited by Poppa Mintin; Mar 5th, 2022 at 05:11 AM. Reason: Added PS
    Along with the sunshine there has to be a little rain sometime.

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How to make pictures transparent on Visual Basic?

    I can't reproduce your problem. I tried .NET Framework 4.8 (VS2019) and 3.5 ClientFramework as well as .NET 6.0 (VS2022). A picture box with a partly transparent BackColor and with a partly transparent Image provided a satisfactory see-through experience in all cases.

    BB

  7. #7
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: How to make pictures transparent on Visual Basic?

    I'm using : -

    Microsoft Visual Studio Community 2019
    Version 16.11.8

    Microsoft .NET Framework
    Version 4.8.04084

    I've just checked that the .png file DOES have transparent areas (In fact most of it is transparent).

    The PictureBox margins are all 0, SizeMode is Stretchimage, it's only 50,50px, when the form opens it's BackColor is the same as that of the form.

    It won't accept BackColor = Transparent. Huh! I just tried that again to get the exact error message and it accepted it ! (maybe I misspelt it previously)
    However, running the code there is still no transparency.

    I've tried:-
    Code:
    Dim tran As Color = My.Resources.ManRed.GetPixel(1, 1)
        Man.TransparencyKey = tran
    '    And
        ManRed.MakeTransparent(backColor)
    Even...
    Code:
    Me.TransparencyKey = tran
    ...doesn't work, that reduces a lot of the Form details (Also a bitmap) tranparent, but Not the flamin' Picturebox's .
    'Man' is the name of the PictureBox.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How to make pictures transparent on Visual Basic?

    I suppose you are aware that both TransparencyKey and Bitmap.MakeTransparent respond only to an exact A, R, G and B combination. If one of these bytes deviates by 1 or more in 256, the rest will remain opaque.

    That aside, I suggest you start from scratch using only the Designer in your preferred VS/Framework. This is just to help find the cause of your problem. Take a Form and give it a small, tiled background image. Then throw on a PictureBox and set its BackColor to something translucent. Finally, put a partly-transparent Image in the PictureBox.

    Here's an example I made this way in the Designer. The dancing mice are the Form's BackgroundImage (default = Tile), the yellow area is the pb BackColor (ARGB values typed to the right of the property), and the ghost is the pb Image.

    Name:  Dancing Mice.jpg
Views: 1178
Size:  35.6 KB

    Does that work for you?

    BB

  9. #9
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: How to make pictures transparent on Visual Basic?

    Just got back to looking at this...

    Took a fresh look and found that I was expecting transparent to work with an image in a picturebox instead of a background image... It's been working all along. !


    I do however still have to type-in 'Transparent', it's still not in the selection menu.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  10. #10

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