vb.net Visual Basic .net 中图像的透明度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/392257/
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 13:59:22  来源:igfitidea点击:

Transparency for images in Visual Basic .net?

vb.netimage

提问by Shane

I have a picture box on my form, which I add a picture to. This picture has a transparent background, but unfortunately, it seems lost in the picture box... I'm guessing that's because the picture box's background colour property is set to grey (the default). I can't see any option for "transparent" though.

我的表单上有一个图片框,我向其中添加了一张图片。这张图片有透明背景,但不幸的是,它似乎丢失了图片框......我猜这是因为图片框的背景颜色属性设置为灰色(默认值)。不过,我看不到任何“透明”选项。

Any idea how I can do it?

知道我该怎么做吗?

回答by user21826

Depending on what you are trying to accomplish there's several different ways to go about it.

根据您要完成的任务,有几种不同的方法可以实现。

Some examples are -

一些例子是 -

Make bitmap transparent

使位图透明

Dim bmp As Bitmap = Bitmap.FromFile("test.bmp")
bmp.MakeTransparent(Color.Magenta) ' magenta in bitmap will be transparent
PictureBox1.Image = bmp

Make the picture box transparent

使图片框透明

PictureBox1.BackColor = Color.Transparent

If you really need a transparent image I would suggest not using the picturebox and just render a transparent bitmap directly.

如果你真的需要一个透明的图像,我建议不要使用图片框,直接渲染一个透明的位图。

回答by TheFlash

If you want the controls behindthe PictureBox to show, ie. you want your image displayed with a transparent background, try drawing straight on the form itself.

如果您希望PictureBox后面的控件显示,即。您希望您的图像以透明背景显示,请尝试直接在表单上绘制。

Back in the days of VB6 we could do this by hooking onto the Form's "Redraw" event or something...

回到 VB6 的时代,我们可以通过挂接到窗体的“重绘”事件或其他东西来做到这一点......

回答by TheFlash

On most controls in VB, in the Backcolor property of the object, there is an option for transparency. This works fine in VB2008, but in VB2005, you must then set it's .parent property to the object behind (well, in my experience anyways.)

在 VB 中的大多数控件上,在对象的背景色属性中,有一个透明度选项。这在 VB2008 中工作正常,但在 VB2005 中,您必须将它的 .parent 属性设置为后面的对象(好吧,无论如何,根据我的经验。)

Hope this helps,

希望这可以帮助,

回答by DMC

I know this is an old topic, but I figured I'd post the solution I came up with for anyone looking for a simple solution for Windows Forms. Say you have PictureBox1 and PictureBox2. You want PictureBox2 to overlay on PictureBox1. Make sure your PictureBox1 background is set to transparent. Then, you can programmatically set:

我知道这是一个老话题,但我想我会发布我想出的解决方案,供任何寻找 Windows 窗体简单解决方案的人使用。假设您有 PictureBox1 和 PictureBox2。您希望 PictureBox2 叠加在 PictureBox1 上。确保您的 PictureBox1 背景设置为透明。然后,您可以以编程方式设置:

PictureBox2.Parent = PictureBox1

Magic

魔法

回答by Rex Mallari Oliveros

Transparent an Image, You can also used Adobe Photoshop to Remove White/Black Background (Watch Tutorial How to Create Transparent Background) or Use this code below,

透明图像,您还可以使用 Adob​​e Photoshop 删除白色/黑色背景(观看教程如何创建透明背景)或使用下面的代码,

PictureBox1.BackColor = Color.Transparent
SetStyle(ControlStyles.SupportsTransparentBackColor, True)