Information fields in image metadata

    Information fields in image metadata

    Some situation require placing the information fields into the metadata of an image you wish to archive. Image metadata is often called Exif (Exchangeable image file format), which is one of many metadata standards.

    Here is an example of adding metadata to an image in C# :

    using System;

    using System.IO;

    using System.Text;

    using System.Windows.Media.Imaging;

    using Newtonsoft.Json;

    // --------------------------------------------------

    public Boolean add_exif( String infile, String outfile, Dictionary<string,string> data )

    {

        if ( !data.ContainsKey( "end" ) )

            data.Add( "end", "of-file" );

        return add_exif( infile, outfile, JsonConvert.SerializeObject( data ) );

    }

    // --------------------------------------------------

    public Boolean add_exif( String infile, String outfile, String json )

    {

        Stream stream = new System.IO.FileStream( infile, FileMode.Open, FileAccess.Read, FileShare.Read );

        BitmapDecoder decoder = null;

        

        bool OK = false;

        string query = null;

        string global_query = null;

        BitmapEncoder encoder = null;

        // https://docs.microsoft.com/en-us/windows/desktop/wic/-wic-native-image-format-metadata-queries

        if ( file.EndsWith( ".png" ) ) {

            decoder = new PngBitmapDecoder( stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default );

            query = "/tEXt/{str=Description}";

            encoder = new PngBitmapEncoder();

        }

        else if ( file.EndsWith( ".jpeg" ) || file.EndsWith( ".jpg" ) ) {

            decoder = new JpegBitmapDecoder( stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default );

            query = "/app1/ifd/exif:{uint=270}";

            encoder = new JpegBitmapEncoder();

        }

        else if ( file.EndsWith( ".tiff" ) || file.EndsWith( ".tif" ) ) {

            decoder = new TiffBitmapDecoder( stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default );

            query = "/ifd/exif:{uint=270}";

            encoder = new TiffBitmapEncoder();

        }

        else if( file.EndsWith( ".gif" ) ) {

            // NB - this doesn't actually work.   the query is for global meta data, and I can't find out how to

            // set global meta data

            decoder = new GifBitmapDecoder( stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default );

            query = "/[0]commentext/TextEntry";

            encoder = new GifBitmapEncoder();

        }

        if ( decoder != null ) {

            

            BitmapFrame frame = decoder.Frames[0];

            BitmapMetadata meta = (BitmapMetadata)frame.Metadata.Clone();

            // These next 2 lines are probably the most important

            meta.SetQuery( query, json );

            encoder.Frames.Add( BitmapFrame.Create( frame, frame.Thumbnail, meta, frame.ColorContexts ) );

           

            Stream output = new FileStream( outfile, FileMode.OpenOrCreate );

            encoder.Save( output );

            output.Close();

            output.Dispose();

            OK = true;

        }

        stream.Close();

        stream.Dispose();

        

        return OK;            

    }