winforms icon indicating copy to clipboard operation
winforms copied to clipboard

PictureBox Background Transparency over any control

Open Amy-Li03 opened this issue 3 years ago • 3 comments

  • .NET Core Version: .NET SDK 7.0.100-preview.2.22103

  • Have you experienced this same bug with .NET Framework?: Yes

Problem description: It is from a suggestion ticket: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1469120 I have a PictureBox filled with a .png image (image with Transparent background) and I place this PictureBox over a ListBox and the result is not what I expected. The PictureBox still shows it’s own background or the Forms’s background, it doesn’t allow me to see through the PictureBox, since my image has a transparent background. image

Expected behavior: The PictureBox should allow customer to see through the PictureBox, since the image has a transparent background.

Minimal repro:

  1. Create a winforms .net core project with a pictureBox control added.
  2. Click the "Choose Image.." link in SmartTag of PictureBox to add a transparent image to it.
  3. Add a ListBox to the form designer, then put PictureBox over this ListBox.
  4. Build and run this project.

Or

  1. Extract and open following test app: TestApp.zip
  2. Build and run this project

Amy-Li03 avatar Feb 23 '22 06:02 Amy-Li03

WinForms does not support transparency across controls, mainly because Windows (GDI) doesn't support cross-window/control transparency very well.

You can enable some window flags to fake it, by asking the system to re-render all adjacent controls and then not overdrawing them, but this comes at a huge performance cost, especially when there are multiple controls on the same window. It also can cause flickering and various incompatibilities with third party controls.

I've tried this before by subclassing controls on Desktop Framework to set this flag, and it had a very poor experience and we weren't happy with it. Unless there's new tricks for cross-control transparency in Windows that I'm not aware of, this is unlikely to work out well.

weltkante avatar Mar 15 '22 10:03 weltkante

This issue is now marked as "up for grabs", and we’re looking for a community volunteer to work on this issue. If we receive no interest in 120 days, we will close the issue. To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

ghost avatar May 05 '22 04:05 ghost

This trick worked before in .NET Framework but in .NET 8 the control is no longer transparent and the Magenta color used for the COLORKEY is now being displayed fully opaque.

NativeMethods.SetWindowLongPtr(this.Handle, NativeMethods.GWL_EXSTYLE, new IntPtr(NativeMethods.WS_EX_LAYERED));
NativeMethods.SetLayeredWindowAttributes(this.Handle, (uint)Color.Magenta.ToArgb(), 0, NativeMethods.LWA_COLORKEY);

vruss avatar Apr 25 '24 03:04 vruss