Advertisement
Willcode4cash

JPG EXIF Metadata - Read/Write

May 23rd, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. public class Sandbox
  2. {
  3.     public void TestMetadata
  4.     {
  5.         var filePath = "c:\\data\\car.jpg";
  6.         FileInfo originalImage = new FileInfo(filePath);
  7.  
  8.         if (originalImage.Exists)
  9.         {
  10.             JpegBitmapDecoder decoder = null;
  11.            
  12.             using (Stream imageStream = File.Open(originalImage.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
  13.             {
  14.                 decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
  15.             }          
  16.  
  17.             var bitmapFrame = decoder.Frames[0];
  18.             var metadata = (BitmapMetadata)bitmapFrame.Metadata;
  19.  
  20.             // SubjectLocation
  21.             // Indicates the location and area of the main subject in the overall scene.
  22.             var subjectLocation = metadata.GetQuery("/app1/ifd/exif:{uint=37396}");
  23.  
  24.             // GPSLatitude
  25.             // Indicates the latitude. The latitude is expressed as three RATIONAL values giving the degrees, minutes,
  26.             // and seconds, respectively. When degrees, minutes and seconds are expressed, the format is dd/1,mm/1,ss/1.
  27.             // When degrees and minutes are used and, for example, fractions of minutes are given up to two decimal places,
  28.             // the format is dd/1,mmmm/100,0/1.
  29.             var gpsLatitude = metadata.GetQuery("/app1/ifd/exif:{uint=2}");
  30.            
  31.             // GPSLongitude
  32.             // Indicates the longitude. The longitude is expressed as three RATIONAL values giving the degrees, minutes,
  33.             // and seconds, respectively. When degrees, minutes and seconds are expressed, the format is ddd/1,mm/1,ss/1.
  34.             // When degrees and minutes are used and, for example, fractions of minutes are given up to two decimal places,
  35.             // the format is ddd/1,mmmm/100,0/1.
  36.             var gpsLongitude = metadata.GetQuery("/app1/ifd/exif:{uint=4}");
  37.            
  38.             // UserComment
  39.             // A tag for Exif users to write keywords or comments on the image besides those in <ImageDescription>, and
  40.             // without the character code limitations of the <ImageDescription> tag.
  41.             var userComment = metadata.GetQuery("/app1/ifd/exif:{uint=37510}");
  42.            
  43.             // ImageDescription:
  44.             // A character string giving the title of the image. It may be a comment such as "1988 company picnic" or the
  45.             // like. Two-bytes character codes cannot be used. When a 2-bytes code is necessary, the Exif Private tag
  46.             // <UserComment> is to be used.
  47.             var imageDescription = metadata.GetQuery("/app1/ifd/exif:{uint=270}");
  48.  
  49.             if (bitmapFrame != null)
  50.             {
  51.                 BitmapMetadata metadataClone = (BitmapMetadata)bitmapFrame.Metadata.Clone();
  52.                 if (metadataClone != null)
  53.                 {
  54.                     metadata.SetQuery("/app1/ifd/exif:{uint=2}", 38.12345);                         // GPS Latitude
  55.                     metadata.SetQuery("/app1/ifd/exif:{uint=4}", -80.54321);                          // GPS Longitude
  56.                     metadata.SetQuery("/app1/ifd/exif:{uint=270}", "This is a test description");   // Image Description
  57.                     metadata.SetQuery("/app1/ifd/exif:{uint=37396}", "101 Main Street, Anytown, USA");  // Subject Location
  58.                     metadata.SetQuery("/app1/ifd/exif:{uint=37510}", "This is a a test comment");           // User Comment
  59.  
  60.                     JpegBitmapEncoder encoder = new JpegBitmapEncoder();
  61.                     encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadataClone, bitmapFrame.ColorContexts));
  62.                     originalImage.Delete();
  63.                     using (Stream jpegStreamOut = File.Open(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
  64.                     {
  65.                         encoder.Save(jpegStreamOut);
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement