How to read GPS metadata from image

Someone asks about how to get GPS (and other) metadata from Image properties. It’s easy once we have Windows Imaging Component, which now the integrate part of .NET framework 3.0, so you can get meta information directly from your image. How to do it? First of all, you should get BitmapMetadata out from your source. In order to do it you should use BitmapDecoder, there are some of them, one for each image type (Jpg, Bmp, Png, Gif etc). So let’s take Jpeg for now.

            using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                JpegBitmapDecoder decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
                BitmapMetadata meta = (BitmapMetadata)decoder.Frames[0].Metadata;
                ulong[] latitude = meta.GetQuery("/app1/ifd/gps/subifd:{ulong=2}") as ulong[];
                ulong[] longitude = meta.GetQuery("/app1/ifd/gps/subifd:{ulong=4}") as ulong[];
            }

Next step is to convert weird ulong array into readable coordinates. Simple calculation in static method for each array and you’r done. I will not enter the math calculation and explain them. Just use it “as-is”. They are rather precise, trust me :)

static double ConvertCoordinate(ulong[] coordinates)
        {
            int lDash = (int)(coordinates[0] - ((ulong)0x100000000));
            int lF = (int)(coordinates[1] - ((ulong)0x100000000));
            double lR = ((double)(coordinates[2] - ((ulong)0x6400000000))) / 100;
            double tRes = (lDash + (((double)lF) / 60)) + (lR / 3600);
            return (Math.Floor((double)(tRes * 1000000)) / 1000000);
        }

We finished, you’r got your coordinates in decimal variation, e.g. 26.34433:37.332344. Have a nice day

Be Sociable, Share!

You may also be interested with:

  1. Video encoder and metadata reading by using Windows Media Foundation

⟨ , ,  ⟩

10 Responses to “How to read GPS metadata from image”

  1. Futbol Argentino Mexicano y espanol » Futbol Argentino Mexicano y espanol ?? re: How to read GPS metadata … Says:

    Pingback from  Futbol Argentino Mexicano y espanol » Futbol Argentino Mexicano y espanol ?? re: How to read GPS metadata …

  2. Futbol Argentino Mexicano y espanol » re: How to read GPS metadata from image Says:

    Pingback from  Futbol Argentino Mexicano y espanol » re: How to read GPS metadata from image

  3. Bruno Santos Says:

    This is just what I was looking for!

    I've downloaded several small apps, just to try to understand how to read that darn GPS coordinates!

    And also the ConvertCoordinate!

    This is the perfect answer for me.

    Thank you very muck.

    Best Regards

  4. Lenny76 Says:

    I've problem with the value of seconds (line double lR = ((double)(coordinates[2] – ((ulong)0×6400000000))) / 100;)

    I receive a very big value different to the correct value.

    is it correct that part of function?

  5. Lenny Says:

    can you check the part of the seconds value?

    I get a strange high value. for example instead of 9,28 I get 49392124020,01

    can you check it?

    thanks.

    lenny

  6. light Says:

    can u post a C# version of this code? :)

  7. Felipe Ceotto Says:

    Hello there.

    I’ve had that done before, which is great, but now I’m looking to do the oposite. I have coordinates and I would like to save them to the image’s metadata but I can’t figure out the correct way of inverting your function. It isn’t really a biunique function but there’s got to be a way of creating the ulong array from regular decimal coordinates. Can you help?

    Thank you!
    Felipe.

  8. Tamir Says:

    You can use the same approach to write, please look into MSDN

  9. Mark Woodlief Says:

    Like the approach but am having issues with the coordinates, and not returning a negative value, for instance, longitude -121.xxxx comes back as 121.xxxx

    wrong part of the world, :)

  10. Balanza Electronica Says:

    Este blog me parece muy interesante, te agregaré a favoritos, adios!

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together