简体   繁体   中英

C# Alpha Blend Transparent PictureBox

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?

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.

In most circumstances OwenP's answer of doing this using the method Bob Powell demonstrated in transcontrols 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.

You could put the PictureBox on another seperate form and use the forms Opacity value in the Windows Style category. Would that work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM