Console: Add GPS metadata to a TIFF file.

Code samples for VintaSoft Imaging .NET SDK. Here you can request a code sample.

Moderator: Alex

Post Reply
Site Admin
Posts: 1876
Joined: Thu Jul 10, 2008 2:21 pm

Console: Add GPS metadata to a TIFF file.

Post by Alex »

This topic contains code sample that shows how to add GPS metadata to a TIFF file:

Code: Select all

/// <summary>
/// Writes the GPS tags into existing TIFF file.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <param name="pageIndex">Index of the page.</param>
private static void WriteGpsTags(string filePath, int pageIndex)
    using (TiffFile tiffFile = new TiffFile(filePath))
        // get the page
        TiffPage page = tiffFile.Pages[pageIndex];
        // find existing GPS image file directory tag
        TiffTag gpsIfdTag = page.IFD.Tags.Find(TiffTagId.GpsIFD);
        // GPS image file directory
        TiffImageFileDirectory gpsIfd;
        // if file doesn't have GPS metadata
        if (gpsIfdTag == null)
            // create GPS image file directory
            gpsIfd = new TiffImageFileDirectory();

            // add necessary tags

            TiffTagCollection gpsTags = gpsIfd.Tags;
            gpsTags.Add((ushort)GpsTagId.GPSVersionID, new byte[] { 3, 2, 0, 0 });
            gpsTags.Add((ushort)GpsTagId.GPSLatitudeRef, "N");
            gpsTags.Add((ushort)GpsTagId.GPSLatitude, GetLatitudeOrLongitude(34.385090));
            gpsTags.Add((ushort)GpsTagId.GPSLongitudeRef, "W");
            gpsTags.Add((ushort)GpsTagId.GPSLongitude, GetLatitudeOrLongitude(118.940933));
            gpsTags.Add((ushort)GpsTagId.GPSAltitudeRef, (byte)0);
            gpsTags.Add((ushort)GpsTagId.GPSAltitude, new TiffRational(14838, 100));
            gpsTags.Add((ushort)GpsTagId.GPSMapDatum, "WGS-84");

            // add GPS image file directory
            page.IFD.Tags.Add(TiffTagId.GpsIFD, gpsIfd);
            gpsIfd = (TiffImageFileDirectory)gpsIfdTag.Data;

            // set data of existing tags and add missing tags the same way as above

/// <summary>
/// Gets the specified latitude or longitude value in degrees
/// as corresponding TIFF tag data.
/// </summary>
/// <param name="degrees">The value of latitude or longitude value degrees.</param>
/// <returns>
/// An array of TIFF RATIONAL containing encoded value,
/// rounded off to whole seconds.
/// </returns>
private static TiffRational[] GetLatitudeOrLongitude(double degrees)
    TiffRational[] result = new TiffRational[3];

    double wholeDegrees = Math.Floor(degrees);
    result[0] = new TiffRational((uint)wholeDegrees, 1);

    double remainingMinutes = (degrees - wholeDegrees) * 60;
    double wholeMinutes = Math.Floor(remainingMinutes);
    result[1] = new TiffRational((uint)wholeMinutes, 1);

    double remainingSeconds = (remainingMinutes - wholeMinutes) * 60;
    double wholeSeconds = Math.Floor(remainingSeconds);
    result[2] = new TiffRational((uint)wholeSeconds, 1);

    return result;

Source codes of console application for VintaSoft Imaging .NET SDK v8.8 can be downloaded here.
Post Reply