C#picturebox透明背景似乎不起作用
对于我的一个项目,我需要以透明背景显示图像。我制作了一些具有透明背景的 .png 图像(为了检查这一点,我在 Photoshop 中打开了它们)。现在我有一个扩展 PictureBox 的类:
class Foo : PictureBox
{
public Foo(int argument)
: base()
{
Console.WriteLine(argument);//different in the real application of course.
//MyProject.Properties.Resources.TRANSPARENCYTEST.MakeTransparent(MyProject.Properties.Resources.TRANSPARENCYTEST.GetPixel(1,1)); //<-- also tried this
this.Image = MyProject.Properties.Resources.TRANSPARENCYTEST;
((Bitmap)this.Image).MakeTransparent(((Bitmap)this.Image).GetPixel(1, 1));
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.BackColor = System.Drawing.Color.Transparent;
}
}
然而,这仅显示具有白色背景的图片框,我似乎无法使其与透明背景一起使用。
For a project of mine I need images to display with a transparent background. I made some .png images that have a transparent background(to check this I opened them in Photoshop). Now I have a class that extends PictureBox:
class Foo : PictureBox
{
public Foo(int argument)
: base()
{
Console.WriteLine(argument);//different in the real application of course.
//MyProject.Properties.Resources.TRANSPARENCYTEST.MakeTransparent(MyProject.Properties.Resources.TRANSPARENCYTEST.GetPixel(1,1)); //<-- also tried this
this.Image = MyProject.Properties.Resources.TRANSPARENCYTEST;
((Bitmap)this.Image).MakeTransparent(((Bitmap)this.Image).GetPixel(1, 1));
this.SizeMode = PictureBoxSizeMode.StretchImage;
this.BackColor = System.Drawing.Color.Transparent;
}
}
this however just displays the picturebox with a white background, I just can't seem to make it work with a transparent background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想将图像覆盖在图像上(而不是图像覆盖在表单上),则可以使用以下方法:
其中 overImage 和 backImage 是带有 png 的 PictureBox(具有透明背景)。
这是因为,如前所述,图像的透明度是使用父容器的背景颜色渲染的。 PictureBoxes 没有“Parent”属性,因此您必须手动创建它(或者当然创建一个自定义控件)。
If you want to overlay images over images (and not images over form), this would make the trick:
Where overImage and backImage are PictureBox with png (with transparent background).
This is because, as said before, the transparency of an image is rendered using the back color of the Parent container. PictureBoxes haven't a "Parent" property so you have to make it manually (or create a cutom control of course).
它可能工作得很好。您正在看到图片框控件背后的内容。这是哪种形式。谁的背景色可能是白色的。您可以设置表单的BackgroundImage 属性,以确保您应该通过图片框看到图像。像这样:
在图片框和上打一个洞 表单需要更大的武器,Form.TransparencyKey
It probably works perfectly. You are seeing what's behind the picture box control. Which is the form. Whose BackColor is probably white. You can set the form's BackgroundImage property to be sure, you should see the image through the picture box. Like this:
Punching a hole through both the picture box and the form requires a bigger weapon, Form.TransparencyKey
CodeProject 网站上有一个出色的解决方案:
制作透明控件 - 无闪烁
本质上,技巧是重写paintbackground事件,以便循环遍历图片框下面的所有控件并重绘它们。其功能是:-
There's an excellent solution on the CodeProject website at
Making Transparent Controls - No Flickering
essentially the trick is to override the paintbackground event so as to loop through all the controls underlying the picturebox and redraw them. The function is:-
我知道您的问题是在 C# 中建立的,但由于相似,&为了便于从 VB.NET 进行转换,我将添加一个完整的 VB 版本,该版本还允许在移动控件时更新控件的背景。
您已经有了答案,但这适用于通过搜索引擎找到这篇文章的其他人,&想要一个 VB 版本,或者只是想找到一个完整的可转换示例(如果他们还需要 C# 版本)。
创建一个新的自定义控件类,&将以下内容粘贴到其中...覆盖默认类内容:
自定义控件类:
...保存类,然后清理您的项目,&再次构建。新控件应显示为新工具项。找到它,&将其拖到您的表单中。
不过,我在使用此控件时遇到了问题...当我尝试加载动画“正在加载”.gif 图像时,就会发生这种情况。
图像没有动画,&当您隐藏控件然后尝试再次显示它时也会出现显示问题。
解决这些问题,&您将拥有一个完美的自定义控制类。 :)
编辑:
我不知道以下内容是否适用于 C# 的 IDE,但这是我的转换尝试:
尝试一下,&我想你自己看看:P
PS:我不是专家,所以预计 C# 和 C# 中都会出现各种错误。 VB.NET 版本。哈哈
I know your Question is founded in C#, but due to the similarity, & ease of conversion from VB.NET, I'll add a full VB version that also allows updating of the control's background as you move it around.
You already have an answer, but this is for others who find this post by search engines, & would like a VB version, or simply want to find a FULL convertible sample if they also need it in C#.
Create a new Custom Control Class, & paste the following into it... overwriting the default class stuff:
Custom Control Class:
...save the class, then clean your project, & build again. The new control should appear as a new tool Item. Find it, & drag it to your form.
I have had issues with this control though... It happens when I try to load an animated "Loading" .gif image.
The image does not animate, & also has display problems when you hide the control, then try to display it again.
Sort those issues out, & you'll have a perfect Custom Control Class. :)
EDIT:
I have no idea if the following will work in C#'s IDE or not, but here's my attempt at conversion:
Try it out, & see for yourself i guess :P
ps: I'm not a guru, so expect all kinds of mistakes in both the C#, & VB.NET versions. lol
如果你在图片框中显示透明的png,它会自动考虑透明度,所以你不需要设置透明颜色
If you are displaying png with transparence in picture box, it will be automatically take transparence into account, so you have no need to set transparent color
上面的答案似乎解决了你的问题。您确实看到了图片框控件背后的内容 - 表单本身的 backColor 为白色。我在这里创建了一个简单的函数,首先将字节类型(数组)的图像转换为位图,然后将特定颜色(来自位图图片)设置为透明。你不妨使用一些东西:
您可以在 pictureBox 的 Paint 上触发此函数。
这是我的类在上面的函数中引用:
谢谢。查格伯特
The above answers seem to solve your problem. You are indeed seeing what's behind the picture box control - the form itself with backColor white. I have here created a simple function that first converts an image of byte type (array) into a bitmap and thereafter setting specific colors (from the bitmap pic) to transparent. Something you might as well use:
You may fire this func on Paint of the pictureBox.
This is my class that is referenced n the function above:
Thanks. Chagbert