Skip to content

Instantly share code, notes, and snippets.

@xleon
Last active November 13, 2020 19:40
Show Gist options
  • Save xleon/7c6e2eff4d9ca3c6db0b to your computer and use it in GitHub Desktop.
Save xleon/7c6e2eff4d9ca3c6db0b to your computer and use it in GitHub Desktop.
Exif rotation fix (Android Xamarin) borrowed from mvvmcross
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;
}
}
@zunjae
Copy link

zunjae commented Jun 26, 2019

Lol thank you! Works great

@joemather
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment