dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.37k stars 968 forks source link

Bitmap class should have transparency informations when loading from file #8825

Open harborsiem opened 3 years ago

harborsiem commented 3 years ago

Laufzeitumgebung: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.103\

Host (useful for support): Version: 5.0.3 Commit: c636bbdc8a

Problem description: Bitmap class has no transparency informations (alpha channel) when loading from file or stream with an alpha channel..

Expected behavior: PictureBoxViews The left side is the current situation. The right side is the expected one.

Minimal repro: TestBitmap.zip See possible solution in file AlphaBitmap.cs

ghost commented 3 years ago

Tagging subscribers to this area: @safern, @tannergooding See info in area-owners.md if you want to be subscribed.

Issue Details
* .NET Core Version: .NET SDK (gemäß "global.json"): Version: 5.0.103 Commit: 72dec52dbd Laufzeitumgebung: OS Name: Windows OS Version: 10.0.19042 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.103\ Host (useful for support): Version: 5.0.3 Commit: c636bbdc8a * Have you experienced this same bug with .NET Framework?: Yes **Problem description:** Bitmap class has no transparency informations (alpha channel) when loading from file or stream with an alpha channel.. **Expected behavior:** ![PictureBoxViews](https://user-images.githubusercontent.com/28147549/107550713-a8d80b80-6bd1-11eb-96dd-d2ccfc3fa509.PNG) The left side is the current situation. The right side is the expected one. **Minimal repro:** [TestBitmap.zip](https://github.com/dotnet/winforms/files/5960339/TestBitmap.zip) See possible solution in file AlphaBitmap.cs
Author: harborsiem
Assignees: -
Labels: `area-System.Drawing`, `untriaged`
Milestone: -
JeremyKuhne commented 3 years ago

This is a request to support this bitmap format in System.Drawing. Clearly GDI+ doesn't recognize this format and it appears that GDI doesn't either.

The suggestion looks worth investigating, but we'd probably want to parse without using any Win32 APIs so this has a chance of working on Unix. I would think we'd want to add an overload with a new option to avoid breaking existing apps that are depending on not getting transparency.

Using Image.LoadFromFile() or Image.LoadFromHBitmap() doesn't work either. Drawing directly to the Graphics object has the same behavior so this has nothing to do with PictureBox.

harborsiem commented 3 years ago

@JeremyKuhne I don't think that an overload of the Bitmap ctor with a new option is a good idea. When using a Png image with alpha channel you will get the transparency direct. When you have a look to a ToolStripButton with the WinForms Designer for example, you will get the image as a resource. When you choose a .bmp file for the Image, you get no alpha channel. But when you choose a .png then you get the alpha channel and the Image looks good.

See the Cut32.png file: Cut_32.png

JeremyKuhne commented 3 years ago

@harborsiem If compatibility wasn't an issue I'd agree with you. I suspect, however, that there are likely bitmaps in user apps that have transparency that, of course, currently don't apply the transparency. Forcing developers to update existing assets needs to have a really good justification behind it, particularly as assets can come indirectly through dependencies that may be out of the developer's ability to update.

From the WinForms perspective this is something I'd feel more comfortable being an opt-in. The System.Drawing owners have to ultimately make the design call.

IshmaZX82 commented 2 years ago

I have the same problem, the intermediate results are stored in bmp format in stream/bytes, and when reading from stream, the alpha channel disappears.