How to set and get Metadata to image using BitmapMetadata class in C#
When Meta data is extracted from TIF images, Meta extractor should consider the directories that Exif tags are located. Exif tags for TIF images are stored in IFD subdirectory.
IPTC Meta data for these image types are stored in sub directories such as irb, 8bimiptc and iptc, which are located in application 13 segment (App 13).
According to these, when Meta data is extracted from different image types, it should define the path correctly by giving application segments if available, sub directories and id of the particular Meta tag otherwise it gives wrong Meta information or errors.
Below shows the code segments for setting and getting Meta data from the JPEG/JPG images
BitmapDecoder decoder = null;
BitmapFrame bitmapFrame = null;
BitmapMetadata metadata = null;
Stream jpegStreamIn;
string path = @"D:\image.jpg"
string jpegDirectory = Path.GetDirectoryName(path);
string output_filename = "image_test.jpg"
string jpegDirectory = Path.GetDirectoryName(path);
string output_filename = "image_test.jpg"
using (jpegStreamIn = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
bitmapFrame = decoder.Frames[0];
metadata = (BitmapMetadata)bitmapFrame.Metadata;
if (bitmapFrame != null)
{
// now get an InPlaceBitmapMetadataWriter, modify the metadata and try to save
InPlaceBitmapMetadataWriter writer = bitmapFrame.CreateInPlaceBitmapMetadataWriter();
writer.SetQuery("/app1/ifd/exif:{uint=306}", "2001:01:01 01:01:01");
if (!writer.TrySave() == true)
{
uint padding = 2048;
BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
if (metaData != null)
{
metaData.SetQuery("/app1/ifd/PaddingSchema:Padding", padding);
metaData.SetQuery("/app1/ifd/exif:{uint=42016}", "ISBN567");
metaData.SetQuery("/app13/irb/8bimiptc/iptc/Category", "Financial Category");
metaData.SetQuery("/app1/ifd/exif:{uint=37394}", "C");
metaData.SetQuery("/app1/ifd/exif:{uint=37395}", "ImageHistory");
metaData.SetQuery("/app1/ifd/exif:{uint=37724}", "About Related Sources");
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
string jpegNewFileName = Path.Combine(jpegDirectory, output_filename);
using (Stream jpegStreamOut = File.Open(jpegNewFileName, FileMode.CreateNew, FileAccess.ReadWrite))
{
encoder.Save(jpegStreamOut);
}
}
jpegStreamIn.Close();
}
}
Now let us turn to see the way of getting Meta data from a JPEG/JPG image{
decoder = new JpegBitmapDecoder(jpegStreamIn, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
bitmapFrame = decoder.Frames[0];
metadata = (BitmapMetadata)bitmapFrame.Metadata;
if (bitmapFrame != null)
{
// now get an InPlaceBitmapMetadataWriter, modify the metadata and try to save
InPlaceBitmapMetadataWriter writer = bitmapFrame.CreateInPlaceBitmapMetadataWriter();
writer.SetQuery("/app1/ifd/exif:{uint=306}", "2001:01:01 01:01:01");
if (!writer.TrySave() == true)
{
uint padding = 2048;
BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone();
if (metaData != null)
{
metaData.SetQuery("/app1/ifd/PaddingSchema:Padding", padding);
metaData.SetQuery("/app1/ifd/exif:{uint=42016}", "ISBN567");
metaData.SetQuery("/app13/irb/8bimiptc/iptc/Category", "Financial Category");
metaData.SetQuery("/app1/ifd/exif:{uint=37394}", "C");
metaData.SetQuery("/app1/ifd/exif:{uint=37395}", "ImageHistory");
metaData.SetQuery("/app1/ifd/exif:{uint=37724}", "About Related Sources");
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts));
string jpegNewFileName = Path.Combine(jpegDirectory, output_filename);
using (Stream jpegStreamOut = File.Open(jpegNewFileName, FileMode.CreateNew, FileAccess.ReadWrite))
{
encoder.Save(jpegStreamOut);
}
}
jpegStreamIn.Close();
}
}
Stream image;
string input_path = @"D:\image_test.jpg"
BitmapDecoder decoder = null;
BitmapFrame bitmapFrame = null;
BitmapMetadata metadata = null;
BitmapFrame bitmapFrame = null;
BitmapMetadata metadata = null;
using (image = File.Open(input_path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
decoder = new JpegBitmapDecoder(image, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
bitmapFrame = decoder.Frames[0];
metadata = (BitmapMetadata)bitmapFrame.Metadata;
string uniqueId = (string)metadata.GetQuery("/app1/ifd/exif:{uint=42016}");
string category = (string)metadata.GetQuery("/app13/irb/8bimiptc/iptc/Category");
string security = (string)metadata.GetQuery("/app1/ifd/exif:{uint=37394}");
string contactInfo = (string)metadata.GetQuery("/app1/ifd/exif:{uint=37395}");
string relationships = (string)metadata.GetQuery("/app1/ifd/exif:{uint=37724}");
image.Close();
Great post explaining Metadata schemas. Has been searching for this collecitve information for a week :-)
ReplyDeleteThanks for sharing this informative blog. Such a useful Blog. I hope keep sharing this type of blog.
ReplyDeleteWebsite Meta Tag Extractor