vb.net 图片框,带有透明背景的 .png 加载在其中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14281370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:33:58  来源:igfitidea点击:

vb.net Picture box with a .png with a transparent background loaded in it?

vb.netimagetransparent

提问by Andy

Ive got a .png file called donkey1.png - it has a transparent background and I have loaded it into a picturebox called pcbDonkey1 - I have changed the properties of the picturebox to have the backcolor transparent - This does not work as it still crosses over another image and has a white background.

我有一个名为 donkey1.png 的 .png 文件 - 它有一个透明的背景,我已经将它加载到一个名为 pcbDonkey1 的图片框 - 我已经更改了图片框的属性以使背景颜色透明 - 这不起作用,因为它仍然交叉另一个图像并具有白色背景。

I've heard about using GDI to draw this image so it will have a transparent image and be able to cross the over image without the white background.

我听说过使用 GDI 来绘制这个图像,所以它会有一个透明的图像,并且能够在没有白色背景的情况下穿过图像。

How would you do this?

你会怎么做?

Thanks

谢谢

回答by user2488689

Just set the background picture to be the parent of the foreground picturebox and the transparancy will work without need for any additional coding

只需将背景图片设置为前景图片框的父级,透明度就可以工作而无需任何额外的编码

     With PictureBox1

        .Image = My.Resources._00_lichaam
        .SizeMode = PictureBoxSizeMode.Zoom
    End With
    With PictureBox2
        .Parent = PictureBox1
        .Image = My.Resources._01_Hoofd
        .SizeMode = PictureBoxSizeMode.Zoom
        .BackColor = Color.Transparent
    End With

That should work

那应该工作

回答by user2488689

Unless you need the PixtureBox control for something more than displaying an image, you can draw the image directly to the form in the Paint event:

除非你需要 PixtureBox 控件来显示图像以外的其他东西,否则你可以在 Paint 事件中直接将图像绘制到窗体上:

Example (you will need to calculate aspect ratio etc., but in principle):

示例(您需要计算纵横比等,但原则上):

Sub Form_Paint(s as Object, e as PaintEventArgs) Handles Me.OnPaint

    Dim r As New Rectangle(myX, myY, myWidth, myHeight)
    e.graphics.DrawImage(myImage, r)

End Sub