Last active
November 13, 2020 19:40
-
-
Save xleon/7c6e2eff4d9ca3c6db0b to your computer and use it in GitHub Desktop.
Exif rotation fix (Android Xamarin) borrowed from mvvmcross
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.Graphics; | |
using Android.Media; | |
private static Bitmap ExifRotateBitmap(string filePath, Bitmap bitmap) | |
{ | |
if (bitmap == null) | |
return null; | |
var exif = new ExifInterface(filePath); | |
var rotation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal); | |
var rotationInDegrees = ExifToDegrees(rotation); | |
if (rotationInDegrees == 0) | |
return bitmap; | |
using (var matrix = new Matrix()) | |
{ | |
matrix.PreRotate(rotationInDegrees); | |
return Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true); | |
} | |
} | |
private static int ExifToDegrees(int exifOrientation) | |
{ | |
switch (exifOrientation) | |
{ | |
case (int)Orientation.Rotate90: | |
return 90; | |
case (int)Orientation.Rotate180: | |
return 180; | |
case (int)Orientation.Rotate270: | |
return 270; | |
default: | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lol thank you! Works great