TIFF: How to add GPS metadata to a TIFF file?
In This Topic
Here is C# code that shows how to add GPS metadata to a TIFF file:
using System;
using System.ComponentModel;
using Vintasoft.Imaging;
using Vintasoft.Imaging.Codecs.ImageFiles.Tiff;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
// create new image
using (VintasoftImage image = new VintasoftImage(100, 100))
{
// save image as a TIFF file
image.Save("test.tif");
}
}
catch (TypeInitializationException ex)
{
if (ex.InnerException is LicenseException)
{
Console.WriteLine(ex.InnerException.Message);
return;
}
throw ex;
}
// add GPS tags to the TIFF file
WriteGpsTags("test.tif", 0);
Console.WriteLine("Press any key...");
Console.ReadKey();
}
/// <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)ExifGpsTagId.GPSVersionID, new byte[] { 3, 2, 0, 0 });
gpsTags.Add((ushort)ExifGpsTagId.GPSLatitudeRef, "N");
gpsTags.Add((ushort)ExifGpsTagId.GPSLatitude, GetLatitudeOrLongitude(34.385090));
gpsTags.Add((ushort)ExifGpsTagId.GPSLongitudeRef, "W");
gpsTags.Add((ushort)ExifGpsTagId.GPSLongitude, GetLatitudeOrLongitude(118.940933));
gpsTags.Add((ushort)ExifGpsTagId.GPSAltitudeRef, (byte)0);
gpsTags.Add((ushort)ExifGpsTagId.GPSAltitude, new TiffRational(14838, 100));
gpsTags.Add((ushort)ExifGpsTagId.GPSMapDatum, "WGS-84");
// add GPS image file directory
page.IFD.Tags.Add(TiffTagId.GpsIFD, gpsIfd);
}
else
{
gpsIfd = (TiffImageFileDirectory)gpsIfdTag.Data;
// set data of existing tags and add missing tags the same way as above
}
tiffFile.SaveChanges();
}
}
/// <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;
}
}
}