问题
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?
回答1:
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 post where 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.
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.
回答2:
In most circumstances OwenP's answer of doing this using the method Bob Powell demonstrated here http://www.bobpowell.net/transcontrols.htm will 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.
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.
回答3:
You could put the PictureBox on another seperate form and use the forms Opacity value in the Windows Style category. Would that work?
来源:https://stackoverflow.com/questions/918353/c-sharp-alpha-blend-transparent-picturebox