Embedding GPS coordinates and other info in JPEG images with C#

GPS and images are a dynamic duo. GPS modules are becoming cheaper and cheaper which means that we would see more gadgets embedded with a GPS as a standard. Cameras and phones are probably the ones that would be on the front line of GPS embedding early adoption. Currently there is the Nokia's N95 cell phone and there might be soon to see camera's with embedded GPS modules (??) and some funny looking , cumbersome integrations .
So having these two modules is a blessing for geotaggers.
What is geotagging you ask? 
Taken from wikipedia
Geotagging, is the process of adding geographical identification metadata to various media such as websites, RSS feeds, or images and is a form of geospatial metadata. This data usually consists of latitude and longitude coordinates, though it can also include altitude, bearing, and place names.


And if you're lucky enough to have a GPS that can track export it's data , you can embed the  GPS coordinated into the JPEG images. Which means that on top of storing things like the camera model and time of picture taken , it can also store the specific location where the picture was taken. And to take it even further , there are image loading web services that locate on a map pictures that were embedded with their respective GPS coordinates . Panoramio.com is my favorite, Flickr used to support this by simply putting the latitude and longitude as tags , but it looks like it is no longer supported. Use Panoramio , it does the job.

Here is for example all of the geotagged images around Rome's Pantheon (from Panoramio).


It appears that it is very simple to read and write these GPS settings from within the JPEG image using .Net's System.Drawing.Image class.
The data stored in JPEG images complies with the EXIF data format. We can use the Image class to read and write data from and to the JPEG image.
here is an example:

Image Pic = Image.FromFile(Filename); 
PropertyItems = Pic.PropertyItems; 
PropertyItems[0].Id = 0x0002; //index of the EXIF TAG 
PropertyItems[0].Type = 5;// 
PropertyItems[0].Len = length; 
PropertyItems[0].Value =new byte[length]; 

And that's pretty much it, there are a few more tweaks to save the image.
I'm publishing here a small C# file with a static function to perform a simple action to embed GPS latitude, longitude (Which can be taken from the gps log according to time that the picture was taken).
It is pretty much straight forward.
It can be used like so

WriteLongLat("c:/temp/house_gps.jpg", 33, 0, 48.46, 35, 5, 38.12);

where the first parameter represents the file name to be embedded and the other parameters represent the lon and lat degrees, minutes, and seconds..
Feel free to use this as will (LGPL) license.

Update :  There is an updated post about the attached example here GPS Coordinates in JPEG Example
Happy Geotagging...

Technorati Profile



  • interesting stuff, thanks.

  • I like this but I have a few questions.

    I coverted this to VB.NET to give it a try and was wondering why I keep getting arthimetic errors when I pass in negative numbers for the deg, min second. For example I want to pass in 40,53,12.88,-111,23,50.28. The -111 fails. Also, do you have a way to convert long/lat to deg/min/sec?


  • Hi, I like this a lot but the thing is that I have some problems make it work in my application in visual studio pro 2005 because the linq libraries, it will be great is you can expand this a little more.

  • Wow, I'm thinking to use it witha a mobile app..
    really an interesting stuff, thanks a lot.

  • How to embed image data in geographic coordinates?

  • I corrected the bug as follows
    // fix for lat
         newProperties[0].Value[4] = 1;
         newProperties[0].Value[12] = 1;
    // fix for lon
         newProperties[1].Value[4] = 1;
         newProperties[1].Value[12] = 1;

  • I tried geotaggin using the above mentined code. But when i try check the lat and lon using exif data viewer it is showing an error. Please help me on this as i have to use it in a very important demo

  • some thing wrong with your link could you please check it.

Comments have been disabled for this content.