dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.06k stars 1.17k forks source link

Cannot create System.Windows.Media.Imaging.BitmapImage from my image file #8252

Open vsfeedback opened 1 year ago

vsfeedback commented 1 year ago

This issue has been moved from a ticket on Developer Community.


This is an image that can be opened normally in applications such as Photo, Paint, Paint 3D, etc. . However, you cannot create a System.Windows.Media.Imaging.BitmapImage from this file. This also prevents the Image from being displayed in WPF's image control. I've attached the project, code, and image files that can be used to reproduce the problem. You should be able to open the image file easily with Windows built-in apps, but you can't create BitmapImage objects from it.


Original Comments

Feedback Bot on 9/7/2023, 11:53 AM:

(private comment, text removed)


Original Solutions

(no solutions)

pchaurasia14 commented 1 year ago

Requesting you to please share the image and the app minimal repro here.

Ebola-Chan-bot commented 1 year ago

WpfApp1.zip In this repo the Image Control can display (223).png but not (94).png.

miloush commented 1 year ago

Please note that (94).png is not a PNG file, it is a JPEG file. It has a color context that is failing to be read with WINCODEC_ERR_STREAMREAD. Opening it with BitmapCreateOptions.IgnoreColorProfile should work as a workaround.

Haven't checked yet whether the file is a valid image file.

Ebola-Chan-bot commented 1 year ago

@miloush The workaround was a success. Better if BitmapImage could handle this situation on its own.

miloush commented 1 year ago

I am going to call this an invalid file. The Exif specification (see e.g. 2.5.4) requires the APP1 segment to include TIFF header. Your file does not include it and as such the APP1 segment is invalid. Removing bytes 0x14 to 0x1D will most likely fix it. WPF does not parse image files, it is handed over to WIC. If you think WIC should be able to handle files malformed in this way, you might need to file a feedback for Windows.

That said, this is not the first issue pointing out issues with invalid color profile or invalid metadata encountered when trying to read color profile (#3884 #3503). We provide the IgnoreColorProfile option, but apart from crawling stack it is impossible for developers to figure out or diagnose that bitmap loading failed during attempt to check for a color profile. It might be worth introducing something like ColorProfileException or similar to enable detecting this situation and try reloading the image without color profile if desirable.

Ebola-Chan-bot commented 1 year ago

My app has the function to display any image provided by the user at runtime. Since it is provided by the user, there is no way to guarantee that the file is completely valid. But users are bound to try to open the file with built-in apps (Photo, etc.). If a user succeeds with the built-in app, he will naturally assume that his image file is valid, and that my app won't open it is my problem. So my app has to deal with that. Now my solution is that, if it fails to open, try again using IgnoreColorProfile

miloush commented 1 year ago

Good, I think if your app is opening external files, you should be doing that either way, possibly informing the user that the color rendering might not be accurate.