How to set and get Metadata to image using BitmapMetadata class in C#

When images are considered, those are storing their Meta data using different types of Meta containers such as EXIF, Adobe XMP, IPTC, Photoshop Image resource and Text descriptions. This every container has their specific unique rules to encode (store) and decode (extract) Meta data. As well as these each Meta container is storing specific Meta information. Exif Meta containers are mostly storing location information and GPS (Global Positioning System) records. As well as, Adobe XMP is responsible to store Dublin core Meta data which provides standard Meta tags that can be used to describe digital objects basically and IPTC is storing Meta information about application records. Meta data extractor is mainly considering these Exif, IPTC and adobe XMP Meta containers when it is extracting Meta data from images.


As well as, these different Meta data containers are considered; they are structuring Meta tags in different directories. JPEG and JPG images has a utility called Application 1 (App1) and all the Exif related tags are stored in the directory called IFD which is located in Application 1 segment. Addition to IFD sub directory there are another sub directories called as ‘InteropIFD’, ‘subIFD’ which are used to store location information and interoperability information. Therefore, when Meta data extractor tool is extracting Exif Meta data from JPEG and JPG images, it should extract Exif Meta data which is stored in right directory (IFD, subIFD, InteropIFD) in application 1 segment. 

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"
   

    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

            Stream image;
            string input_path = @"D:\image_test.jpg"
            BitmapDecoder decoder = 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();
 

Comments

  1. Great post explaining Metadata schemas. Has been searching for this collecitve information for a week :-)

    ReplyDelete
  2. Thanks for sharing this informative blog. Such a useful Blog. I hope keep sharing this type of blog.

    Website Meta Tag Extractor

    ReplyDelete

Post a Comment

Popular posts from this blog

How to add standard and custom Metadata to PDF using iTextSharp

How to generate direct line token to start a new conversation between your own client application and bot framework in Java