Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vb.net - Transparency of picture box

Im just looking for an answer in my simple problem. Here it is

I have a pricturebox that has image with transparent background i Set the picturebox backcoloras transparent.

and after that, the picture has transparent bg. But after i added this code

ìmg1.Left = windows.forms.cursor.Position.X - me.Left ìmg1.Top= windows.forms.cursor.Position.Y - me.Top

'code for using img as cursor

the image bg is not really transparent like this

enter image description here

I think the transaparent backcoloris not really transparent. it will only get the backcolorof form and use it as backcolorof image instead of transparent.

Is there any solution to make it fully transparent?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are correct in your assumption.
Transparency in winforms does not mean that the object is actually transparent. Instead, it means that it will display it's parent object instead of it's background, including it's background, images and text, but not including any other controls on it, hence your problem.
Since the parent control of your top most picture box is not and can not be the other picture boxes, the fact that your top most picture box have a transparent background will not help.

Unfortunately, using the form's TransparencyKey property will also not help for this. (It will make the selected color transparent, but will yield unexpected (and usually undesired) results.

In order to achieve your goal, you will have to follow OneFineDay's advice in the comments, and use Graphics to draw the image yourself.
Fortunately, this is very easy to do:

Public Sub DrawImage(Image as Image, Location As Point)
    Using(Dim g as Graphics = Me.CreateGraphics())
        g.DrawImage(Image, Location)
    EndUsing
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...