Images captured by iOS show up rotated on Android - Mon, Jul 23, 2018
When you capture a photo with TakePhotoAsync
on the iOS, regardless of the SaveMetaData
flag or RotateImage
, it might be stored rotated. However, you won’t notice it on the iOS itself, or on Windows, since the orientation information is stored in the EXIF metadata of the captured JPEG file.
iOS or Windows show the image correctly because they take into account the EXIF orientation, but the Image view of Xamarin.Forms on Android ignores this flag, thus making the image show up as-is, i.e. rotated.
If you store the image in a different format than JPEG (e.g. PNG) on the iOS, you can also see the image rotated there, because PNG doesn’t have an orientation flag.
If you are using Xamarin.Forms, consider using CachedImage
from FFImageLoading
instead of the built-in Image view. CachedImage
looks at the orientation information on Android, and rotates the image accordingly before rendering it.
However, if you are uploading the image to an ASP.NET core application and need to process it, you can rotate it manually based on the EXIF information. .NET core by itself doesn’t have image manipulation capabilities, but you can install the CoreCompat.System.Drawing
NuGet package, which is a port of System.Drawing for .NET core.
First, load the image as an Image
object:
var image = Image.FromFile(path);
Then see if it has the rotation value, and rotate accordingly:
if (image.PropertyIdList.Contains(0x112))
{
int rotationValue = image.GetPropertyItem(0x112).Value[0];
if (rotationValue == 8)
image.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);
else if (rotationValue == 3)
image.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);
else if (rotateValue == 6)
image.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);
}
Optionally, you can save the image on the original file:
image.Save(path);