Click here to Skip to main content
15,892,517 members
Articles / Programming Languages / C#
Tip/Trick

Extracting Exif Coordinate From Geotagged Photo

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
24 Jul 2014CPOL1 min read 11.1K   5  
Reading Exif coordinates from image for Windows phone 8

Introduction

This tip shows you how to read Exif coordinates from image for Windows phone 8.

I have gone round and round trying to find a plug and play solution for reading exif information from every corner of the internet and by reading the few books I have access to without much success.

I decided to come up with my own plug and play version of an exif reader, thanks to the C# implementation of an EXIF Library available on CodeProject, by Tim Heuer(Good Man!). Works well for both Windows Phone 7 and 8.

So I combined the foundational work by Igor Ralic and Colin Eberhardt to create this simple class for accessing geocode information from images.

So accessing Exif data has never been a piece of cake with this awesome one liner call to the JpegInfo class.

C#
JpegInfo info = ExifLib.ExifReader.ReadJpeg(e.ChosenPhoto, e.OriginalFileName);

The JpegInfo provides direct access to the exif data which you can create your own objects for storing the data and use how you want! I know you are crazy with the blah blah blah – so here is the code:

C#
        void task_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                photoLoaded = true;
                JpegInfo info = ExifLib.ExifReader.ReadJpeg(e.ChosenPhoto, e.OriginalFileName);
                var img = new BitmapImage();
                img.SetSource(e.ChosenPhoto);
                loadedImg.Source = img;
                ReadExif(info);
            }
        }
        private static double DecodeLatitude(JpegInfo info)
        {
            double degrees = ToDegrees(info.GpsLatitude);
            return info.GpsLatitudeRef == ExifGpsLatitudeRef.North ? degrees : -degrees;
        }

        private static double DecodeLongitude(JpegInfo info)
        {
            double degrees = ToDegrees(info.GpsLongitude);
            return info.GpsLongitudeRef == ExifGpsLongitudeRef.East ? degrees : -degrees;
        }

        public static double ToDegrees(double[] coord)
        {
            return coord [0] + coord [1] / 60.0 + coord [2] / (60.0 * 60.0);
        }
        private void ReadExif(JpegInfo info)
        {
            var Lat = 0.0;
            var Long = 0.0;
            // check if we have co-ordinates
            if (info.GpsLatitude.First() != 0.0)
            {
                    Lat = DecodeLatitude(info);
                    Long = DecodeLongitude(info);
            }
}

Something this like has never been easier to find. And there you go. Plug and play it in your own code and let me know how it goes. You can contact me at omondiy@gmail.com and my blog http://datawebke.blogspot.com/.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --