I want to fetch the information from the image regarding the Geolocation as shown in the image below
None of these answers seemed to be completely working and correct. Here's what I came up with using this EXIF library, which is also available as a NuGet package.
public static double[] GetLatLongFromImage(string imagePath)
{
ExifReader reader = new ExifReader(imagePath);
// EXIF lat/long tags stored as [Degree, Minute, Second]
double[] latitudeComponents;
double[] longitudeComponents;
string latitudeRef; // "N" or "S" ("S" will be negative latitude)
string longitudeRef; // "E" or "W" ("W" will be a negative longitude)
if (reader.GetTagValue(ExifTags.GPSLatitude, out latitudeComponents)
&& reader.GetTagValue(ExifTags.GPSLongitude, out longitudeComponents)
&& reader.GetTagValue(ExifTags.GPSLatitudeRef, out latitudeRef)
&& reader.GetTagValue(ExifTags.GPSLongitudeRef, out longitudeRef))
{
var latitude = ConvertDegreeAngleToDouble(latitudeComponents[0], latitudeComponents[1], latitudeComponents[2], latitudeRef);
var longitude = ConvertDegreeAngleToDouble(longitudeComponents[0], longitudeComponents[1], longitudeComponents[2], longitudeRef);
return new[] { latitude, longitude };
}
return null;
}
Helpers:
public static double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds, string latLongRef)
{
double result = ConvertDegreeAngleToDouble(degrees, minutes, seconds);
if (latLongRef == "S" || latLongRef == "W")
{
result *= -1;
}
return result;
}
public static double ConvertDegreeAngleToDouble(double degrees, double minutes, double seconds)
{
return degrees + (minutes / 60) + (seconds / 3600);
}
Credit to Igor's answer for the helper method and geedubb's for the main method.