Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello, even i already set the color of background for a picture box to transparent
but i found that, it was not complete transparent

i made two picturebox.
if picturebox1 overlay on picturebox 2
i found that the picturebox1 actually was not transparent
but it was using the backcolor of the form's color

is there any method to make it transparent?
even if the picturebox1 was overlay on the other picturebox ,
the transparent part will show to image of other picturebox..

What I have tried:

Dim _panel As New Panel

With _panel
.Location = New Point(250, 125)
.Size = New Size(1116, 593)
.BackColor = Color.Transparent
End With

.Controls.Add(_panel)

Dim _referralButton As New PictureBox

With _referralButton

.SizeMode = PictureBoxSizeMode.StretchImage
.Size = New Size(200, 200)
.BackColor = Color.Transparent
.Location = New Point((_panel.Width - _referralButton.Width) / 2, _
(_panel.Height - _referralButton.Height) / 2)
.Image = Image.FromFile("C:\Leech\img\Referral.png")
End With

' _panel.Controls.Add(_referralButton)

Dim _Button As New PictureBox

With _Button
.SizeMode = PictureBoxSizeMode.StretchImage
.Size = New Size(150, 150)
.BackColor = Color.Transparent
.Image = Image.FromFile("C:\Leech\img\Button.png")
.Location = New Point(_panel.Width / 4 + _Button.Width / 4, _
(_panel.Height * 3 / 4) - (_Button.Height * 3 / 4))
End With

_panel.Controls.Add(_Button)
Posted
Updated 1-Jun-16 6:32am
v2

As you've found out, Transparent isn't.

Transparent tells the control to take on the background properties of it's parent container, giving the ILLUSION of transparency. If the form BackColor is Blue and you tell a Button on the form to use Transparent, this tells the button to take on the BackColor property of the Form, turning the background of the Button to Blue.

It does NOT make the control transparent.
 
Share this answer
 
A picture box can't normally be transparent, so I'd recommend that you don't use them but instead use a single Panel, and draw the images directly in the Panel.Paint event- but it is possible if you fudge the system a bit, by changing the parent of the overlapping PictureBox to the one below instead of the Form itself. You can;t do that in teh designer, but you can so it in the Form constructor:
VB
Public Sub New()
	InitializeComponent()
	pbAtTheBottom.Controls.Add(pbOnTop)
	pbOnTop.Location = New Point(0, 0)
	pbOnTop.BackColor = Color.Transparent
End Sub
Should do it, but I haven't tested it in VB, just C#.
 
Share this answer
 
Comments
newbie1992 1-Jun-16 7:37am    
thank you sir for your help
but in this case i add my picturebox in a form

for an example
the size of picturebox1 was 50 , 50
and the location was 0 , 0

the size of picturebox2 was same as picturebox1
and the location was 40 , 40

in this case the picturebox was overlay on picturebox2

i have try the method suggest by u sir but i still does not work
the 'transparent' part of the picturebox1 still doesn't show the image of picturebox2,
but it show the color of the original form

*sorry for my broken english
OriginalGriff 1-Jun-16 7:50am    
Then you have probably added picture box 1 to picture box 2 rather than the other way round...
newbie1992 1-Jun-16 8:09am    
i have just update my question with the source code i have done
i don't think i have added the picturebox1 into picturebox2

i add both of it into a layer ( panel)
OriginalGriff 1-Jun-16 8:26am    
So...you didn't bother to read what I said at all then?
PictureBox is not suitable here. You need to render the image directly on your custom control or some suitable available control, like Panel. To do so, you need to handle the event Paint, or, in your derived class, override the virtual method OnPaint. For drawing, use the instance of System.Drawing.Graphics reference passed in even arguments parameter. Of course, transparency makes sense if 1) your image itself supports transparency and have semi-transparent pixels (with opacity less then 1) and 2) your background is not uniform so you have something to see through.

After all, transparency should be considered as a sort of metaphor. On screen, each resulting pixel is shown and is 100% opaque. Opacity (alpha value) of a pixel in pixel types supporting alpha channel is used to get final combined pixel value.

Please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

Also, perhaps it's worth noting that PictureBox is heavily abused by many. This control is not image or a picture, this is a totally redundant control rendering an image, which can be easily implemented on the application level. It's only value is the shortcut in code, just using this available control type for the simplest chores, showing a static image without any effects. An attempt to do anything a bit more complicated is a total waste. Please see my past answers:
Append a picture within picturebox[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900