Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sandbox
- {
- public void TestMetadata
- {
- var filePath = "c:\\data\\car.jpg";
- FileInfo originalImage = new FileInfo(filePath);
- if (originalImage.Exists)
- {
- JpegBitmapDecoder decoder = null;
- using (Stream imageStream = File.Open(originalImage.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
- {
- decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
- }
- var bitmapFrame = decoder.Frames[0];
- var metadata = (BitmapMetadata)bitmapFrame.Metadata;
- // SubjectLocation
- // Indicates the location and area of the main subject in the overall scene.
- var subjectLocation = metadata.GetQuery("/app1/ifd/exif:{uint=37396}");
- // GPSLatitude
- // Indicates the latitude. The latitude is expressed as three RATIONAL values giving the degrees, minutes,
- // and seconds, respectively. When degrees, minutes and seconds are expressed, the format is dd/1,mm/1,ss/1.
- // When degrees and minutes are used and, for example, fractions of minutes are given up to two decimal places,
- // the format is dd/1,mmmm/100,0/1.
- var gpsLatitude = metadata.GetQuery("/app1/ifd/exif:{uint=2}");
- // GPSLongitude
- // Indicates the longitude. The longitude is expressed as three RATIONAL values giving the degrees, minutes,
- // and seconds, respectively. When degrees, minutes and seconds are expressed, the format is ddd/1,mm/1,ss/1.
- // When degrees and minutes are used and, for example, fractions of minutes are given up to two decimal places,
- // the format is ddd/1,mmmm/100,0/1.
- var gpsLongitude = metadata.GetQuery("/app1/ifd/exif:{uint=4}");
- // UserComment
- // A tag for Exif users to write keywords or comments on the image besides those in <ImageDescription>, and
- // without the character code limitations of the <ImageDescription> tag.
- var userComment = metadata.GetQuery("/app1/ifd/exif:{uint=37510}");
- // ImageDescription:
- // A character string giving the title of the image. It may be a comment such as "1988 company picnic" or the
- // like. Two-bytes character codes cannot be used. When a 2-bytes code is necessary, the Exif Private tag
- // <UserComment> is to be used.
- var imageDescription = metadata.GetQuery("/app1/ifd/exif:{uint=270}");
- if (bitmapFrame != null)
- {
- BitmapMetadata metadataClone = (BitmapMetadata)bitmapFrame.Metadata.Clone();
- if (metadataClone != null)
- {
- metadata.SetQuery("/app1/ifd/exif:{uint=2}", 38.12345); // GPS Latitude
- metadata.SetQuery("/app1/ifd/exif:{uint=4}", -80.54321); // GPS Longitude
- metadata.SetQuery("/app1/ifd/exif:{uint=270}", "This is a test description"); // Image Description
- metadata.SetQuery("/app1/ifd/exif:{uint=37396}", "101 Main Street, Anytown, USA"); // Subject Location
- metadata.SetQuery("/app1/ifd/exif:{uint=37510}", "This is a a test comment"); // User Comment
- JpegBitmapEncoder encoder = new JpegBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metadataClone, bitmapFrame.ColorContexts));
- originalImage.Delete();
- using (Stream jpegStreamOut = File.Open(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
- {
- encoder.Save(jpegStreamOut);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement