dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.86k stars 1.68k forks source link

IImage does not take into account photo Orientation, causing IImage height and width to get mixed up #23391

Open jonmdev opened 2 weeks ago

jonmdev commented 2 weeks ago

Description

When you take cell phone picture, the phone will typically store the orientation of the phone inside the metadata. This instructs systems whether it should be displayed tall or wide.

In Maui, if you take a photo by filepicker from the file system and set it to an image like:

var stream1 = await fileResult.OpenReadAsync();
var stream2 = await fileResult.OpenReadAsync();

imageSource = ImageSource.FromStream(() => stream1);
image.Source = imageSource;

iimage = PlatformImage.FromStream(stream2);
rawImageHeight = iimage.Height;
rawImageWidth = iimage.Width;
Debug.WriteLine("IIMAGE HEIGTH " + rawImageHeight);
Debug.WriteLine("IIMAGE WIDTH " + rawImageWidth);
Debug.WriteLine("IIMAGE H/WIDTH " + rawImageHeight/rawImageWidth);

image.SizeChanged += delegate {
        Debug.WriteLine("IMAGE WIDTH " + image.Width + " Height " + image.Height + " h/w " + image.Height/(double)image.Width );

};

Then if you compare, with a cell phone photo taken in portrait, you get the Image displaying this correctly, in UI scaled units:

Width: 300.1904761904762 Height: 400 h/w: 1.3324873096446699

This shows it is displaying it taller than wide as expected for a portrait photo.

However, IImage returns the wrong info:

IIMAGE HEIGHT: 3000 IIMAGE WIDTH: 4000 IIMAGE H/WIDTH: 0.75

IImage gets it inverted. It has not taken into account the "orientation" likely of the photo so now thinks it is wider than it is tall.

This makes using IImage impractical for resizing or judging expected file dimensions or anything really. It will work fine when thereis no orientation data (eg. a phone screencap) but a real photo causes inversion of height and width parameters.

Can IImage be fixed or is there any suggestion to make it work? How is Image taking into account the orientation data? Do we have any way to read the Orientation EXIF so we can correct for it ourselves with IImage?

Orientation is discussed here somewhat: https://stackoverflow.com/questions/9371273/how-can-i-display-the-orientation-of-a-jpeg-file

I see it claimed that Image should be able to tell us the orientation but I don't know how as the info seems outdated: https://www.cyotek.com/blog/handling-the-orientation-exif-tag-in-images-using-csharp

Solution

Ideally IImage should automatically read the orientation in the same way as Image/ImageSource does so that these create identical outcomes.

Otherwise some method to read it manually and correct manually would work as well.

Any suggestions? Or is IImage just not useful and should be abandoned then?

Steps to Reproduce

1) Take a photo with camera in portrait and landscape each. 2) Try using FilePicker as discussed here https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-picker?view=net-maui-8.0&tabs=android 3) Assign stream to ImageSource and Image (normal result) and assign to IImage (mixes H/W)

Link to public reproduction project repository

I can post if it helps

Version with bug

8.0.61 SR6.1

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 2 weeks ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.