Search code examples
c#geolocationwindows-phoneexifexiflib

How to Fetch the Geotag details of the captured image or stored image in Windows phone 8


I want to fetch the information from the image regarding the Geolocation as shown in the image below

enter image description here

 void cam_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {

                Image cameraImage = new Image();
                BitmapImage bImage = new BitmapImage();

                bImage.SetSource(e.ChosenPhoto);
                cameraImage.Source = bImage;

                e.ChosenPhoto.Position = 0;

                ExifReader reader = new ExifReader(e.ChosenPhoto);
                double gpsLat, gpsLng;

                reader.GetTagValue<double>(ExifTags.GPSLatitude,
                                                    out gpsLat))

                reader.GetTagValue<double>(ExifTags.GPSLongitude,
                                                    out gpsLng))


                MessageBox.Show(gpsLat.ToString() + "" + gpsLng.ToString());   

            }
        }

so that we can detect location where the image was taken. Please help to find the these property.


Solution

  • You will need to read the EXIF data from the image.

    You can use a library such as this

    // Instantiate the reader
    ExifReader reader = new ExifReader(@"..path to your image\...jpg");
    
    // Extract the tag data using the ExifTags enumeration
    double gpsLat, gpsLng;
    if (reader.GetTagValue<double>(ExifTags.GPSLatitude, 
                                        out gpsLat))
    {
        // Do whatever is required with the extracted information
        //...
    }
    if (reader.GetTagValue<double>(ExifTags.GPSLongitude, 
                                        out gpsLng))
    {
        // Do whatever is required with the extracted information
        //...
    }
    

    UPDATE. Code changed to use MemoryStream

        void cam_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                using (MemoryStream memo = new MemoryStream())
                {
                    e.ChosenPhoto.CopyTo(memo);
                    memo.Position = 0;
                    using (ExifReader reader = new ExifReader(memo))
                    {
                        double[] latitudeComponents;
                        reader.GetTagValue(ExifTags.GPSLatitude, out latitudeComponents);
    
                        double[] longitudeComponents;
                        reader.GetTagValue(ExifTags.GPSLongitude, out longitudeComponents);
    
                        // Lat/long are stored as D°M'S" arrays, so you will need to reconstruct their values as below:
                        var latitude = latitudeComponents[0] + latitudeComponents[1] / 60 + latitudeComponents[2] / 3600;
                        var longitude = longitudeComponents[0] + longitudeComponents[1] / 60 + longitudeComponents[2] / 3600;
    
                        // latitude and longitude should now be set correctly...
                    }
                }
            }
        }