Using the class EXIFExtractor
implemented and explained here, thus:
var bmp = new Bitmap(pathToImageFile);
var exif = new EXIFextractor(ref bmp, "n");
if (exif["Orientation"] != null)
{
RotateFlipType flip = OrientationToFlipType(exif["Orientation"].ToString());
if (flip != RotateFlipType.RotateNoneFlipNone) // Se a orientação já está correta
{
bmp.RotateFlip(flip);
exif.setTag(0x112, "1");
bmp.Save(pathToImageFile, ImageFormat.Jpeg);
}
}
private static RotateFlipType OrientationToFlipType(string orientation)
{
switch (int.Parse(orientation))
{
case 1:
return RotateFlipType.RotateNoneFlipNone;
break;
case 2:
return RotateFlipType.RotateNoneFlipX;
break;
case 3:
return RotateFlipType.Rotate180FlipNone;
break;
case 4:
return RotateFlipType.Rotate180FlipX;
break;
case 5:
return RotateFlipType.Rotate90FlipX;
break;
case 6:
return RotateFlipType.Rotate90FlipNone;
break;
case 7:
return RotateFlipType.Rotate270FlipX;
break;
case 8:
return RotateFlipType.Rotate270FlipNone;
break;
default:
return RotateFlipType.RotateNoneFlipNone;
}
}
I took the example from here.
Without EXIF I think there’s no way.
Good answer. : ) Indeed has as do without EXIF, but it’s not very trivial. An option that is certainly simpler and maybe works well enough (I’m not sure, the AP needs to test) should be to use Opencv to estimate the pose (the example is in Python, but should work in the size of C#) of some object in the image, and then decide the orientation from it.
– Luiz Vieira
@Luizvieira I once tried to use Opencv for image recognition. It was so complicated that I gave up.
– Leonel Sanches da Silva
Hahahaha I totally understand you. But don’t worry, it happens to anyone who uses Opencv. :)
– Luiz Vieira