in

ASP.NET Weblogs

This Blog

Syndication

Sponsors

Roiy Zysman

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.

image 

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).

image

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]; 
Pic.SetPropertyItem(PropertyItems[0]);

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...
Roiy

Technorati Profile

 

Published Jul 13 2008, 01:33 AM by zroiy
Filed under: , , , ,

Comments

 

Guy Harwood said:

interesting stuff, thanks.

July 15, 2008 10:01 AM
 

John said:

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?

John

July 24, 2008 1:06 PM
 

efloresp said:

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.

July 29, 2008 7:38 PM
 

Dr.Luiji said:

Wow, I'm thinking to use it witha a mobile app..

really an interesting stuff, thanks a lot.

February 5, 2009 3:36 PM
 

max said:

How to embed image data in geographic coordinates?

March 13, 2009 12:23 PM
 

bug in lat/long said:

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;

April 23, 2009 5:57 PM
 

Venkat said:

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

February 7, 2010 7:27 AM
 

mohi88 said:

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

October 12, 2011 8:05 AM

Leave a Comment

(required)  
(optional)
(required)  
Add