C# Alpha Blend 透明图片框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/918353/
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
C# Alpha Blend Transparent PictureBox
提问by PeteT
I have a webcam feed being displayed on my form and would like to draw on top of it. I was going to do this using a picture box, I have found the PictureBox doesn't support true transparency instead just the colour of the form it's on. It also doesn't support alphablending which is how I would want it to display, similar to how forms can be made to appear.
我的表单上显示了一个网络摄像头提要,并想在其上绘图。我打算使用图片框来做到这一点,我发现图片框不支持真正的透明度,而只是它所在表单的颜色。它也不支持字母混合,这是我希望它显示的方式,类似于如何显示表单。
Does anyone know how to do this? Or have a control implemented that can do this?
有谁知道如何做到这一点?或者有一个可以做到这一点的控件?
采纳答案by PeteT
In most circumstances OwenP's answer of doing this using the method Bob Powell demonstrated here http://www.bobpowell.net/transcontrols.htmwill work. However because it was a direct draw interface to the webcam the live feed always seemed to be on top of the other controls.
在大多数情况下,OwenP 使用 Bob Powell 在此处演示的方法来执行此操作的答案http://www.bobpowell.net/transcontrols.htm将起作用。然而,因为它是网络摄像头的直接绘图界面,所以实时馈送似乎总是在其他控件之上。
To solve this I cheated a little and created a secondary form that opens over the video feed this had no borders, was set to always on top and given transparency, I drew directly onto this form. I then just had to handle a few events in this secondary form to keep focus on the original form and handle when another program is in use.
为了解决这个问题,我做了一点点作弊,并创建了一个辅助表单,它可以在没有边框的视频源上打开,设置为始终在顶部并提供透明度,我直接在此表单上绘制。然后我只需要处理这个辅助表单中的一些事件,以保持对原始表单的关注并在另一个程序正在使用时进行处理。
回答by PeteT
You could put the PictureBox on another seperate form and use the forms Opacity value in the Windows Style category. Would that work?
您可以将 PictureBox 放在另一个单独的窗体上,并使用 Windows 样式类别中的窗体 Opacity 值。那行得通吗?
回答by OwenP
Transparency in Windows Forms is kind of broken. As you have discovered, it doesn't really support overlapping controls; it just shows the form's background color through the transparent parts. With some Windows API magic, you can make many controls display as truly transparent with support for overlapping, as this article by Bob Powell demonstrates. I've been able to make his technique work for labels and custom controls, but I think I've tried to make it work for PictureBox
with disappointing results. One workaround could be try create a custom control that manually draws the image; I haven't tried. Here's a forum postwhere I demonstrate a custom control that supports true transparency (my handle on that forum is AtmaWeapon.) My memory seems to suggest that this works best for custom controls due to some apparent magic that happens with Microsoft's controls when they are rendered.
Windows 窗体中的透明度有点破损。正如您所发现的,它并不真正支持重叠控件;它只是通过透明部分显示表单的背景颜色。使用一些 Windows API 魔法,您可以使许多控件显示为真正透明并支持重叠,正如Bob Powell 的这篇文章所展示的那样。我已经能够使他的技术适用于标签和自定义控件,但我想我已经尝试使其适用于PictureBox
令人失望的结果。一种解决方法是尝试创建一个手动绘制图像的自定义控件;我没试过 这是一个论坛帖子在那里我演示了一个支持真正透明的自定义控件(我在该论坛上的句柄是 AtmaWeapon。)我的记忆似乎表明这对自定义控件最有效,因为 Microsoft 控件在呈现时会发生一些明显的魔法。
Alternatively, you could implement your application using WPF instead of WinForms. WPF's transparency works and supports overlapping controls. This is a pretty big shift in how you'd develop your application, but it works from the start.
或者,您可以使用 WPF 而不是 WinForms 来实现您的应用程序。WPF 的透明度有效并支持重叠控件。这是您开发应用程序方式的一个相当大的转变,但它从一开始就有效。