Change image orientation for iPhone and/or Android phone pics in .NET C#

Correggere l'orientamento delle foto scattate con iPhone e/o Android con .NET C#

If you’re app developers and/frequently work with images coming from mobile devices, you most likely already know about the photo orientation issue: you can see the picture properly oriented from the smartphone or tablet, yet you get it rotated by 90, 180 or 270 degrees when you open it with your desktop viewer, library or component.

The issue

The reason for that is quite trivial: whenever a photo is taken, the device saves the current accelerometer orientation into a corresponding Exif tag within the image metadata, so the viewer apps can retrieve it and rotate the photo accordingly. Problem is, this handy auto-correction feature is often not present in most softwares, libraries and components for desktop computers, meaning that they will basically show you the image as it has been taken, which is often very wrong from your (or your customer) point of view.

The reasons for such behaviour are very simple: almost any image viewing/editing software made for desktop computers is made to support all types of images, not just photos, so they won’t fetch any Exif tag to modify your viewing/editing experience unless you tell them to. On top of that, most of them aren’t accustomed to digital images coming from accelerometer-enabled devices such as smartphones and tablets, so they won’t bother checking for the Orientation tag whatsoever.

The same scenario applies to image SDKs, components tools and libraries, including native solutions such as GDI+ e DirectX: they won’t automatically use any Exif tag info to correct anything, including the image orientation. For example, when we do something like the following:

we can be sure that the resulting   will have the exact same orientation issues you can experience with any desktop PC image viewer.

The solution

Luckily enough, the fix for this issue is fairly simple: we only need to write a simple C# helper method that can fetch the Orientation Exif tag (  ), the one that stores the device orientation status, and rotate the image accordingly. The required code can be summarized into the following helper class:

We can see that the helper method includes two different overloads: the former one to support file paths, the latter accepting an existing    object.

It’s also worth noticing how both methods optionally allow the developer to remove the Orientation Exif tag if the image gets rotated, which is usually a good thing to do: if we fix the issue by physically rotating the image we should also remove any potentially misleading info that might trigger the auto-adjustement feature of any orientation-aware viewer and/or app.

That’s all for now: happy orientation!

RELATED POSTS

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan