locked
How To Read EXIF & IPTC Meta Data From JPG. RRS feed

  • Question

  • User1857633543 posted

    Hi Folks.

    Here is a quick guide on how to read a JPG's EXIF & IPTC Meta Data.

    Step 1: Add the references PresentationCore WindowsBase to your web config.

    Step 2: Create a class file with following:

    using System.IO;
    using System.Windows.Media.Imaging;
    
    public class image_managment_class
    {
       // Generic File Stream Object
        protected FileStream _fileStream(string s)
        {
            FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(s), FileMode.Open, FileAccess.Read, FileShare.Read);
            return fs;
        }
        // Generic BitmapSource Object
        protected BitmapSource _bitmapSource(string s)
        {
            BitmapSource bs = BitmapFrame.Create(_fileStream(s));
            return bs;
        }
        // BitmapMetadata Object
        public BitmapMetadata _returnBitmapMetaData(string s)
        {
            // s = file virtual path
            BitmapMetadata md = (BitmapMetadata)_bitmapSource(s).Metadata;
            return md;
        }
    
    }

    Now all you need to do to referance an propertie is


    // EXIF
    image_managment_class IM = new image_managment_class();
    string image_title = IM.returnBitmapMetaData("~/file.jpg").Title;
    
    // Hit intellisense after dot for list of EXIF properties.
    
    // IPTC - little harder.
    string date_created  = IM._returnBitmapMetaData("~/file.jpg").GetQuery(@"/app13/irb/8bimiptc/iptc/date created").ToString();
    
    

    To extend this to return a more indepth blue print add the following to your image_managment_class file


        // Create a Image BluePrint
        public class OneImageBluePrint
        {
            public string VirtualFilePath { get; set; }
            public string Title { get; set; }
            public string Subject { get; set; }
            public string[] Keywords { get; set; }
            public string Author { get; set; }
            public string DateAdded { get; set; }
            public string Copyright { get; set; }
            public string LocationPlace { get; set; }
            public string LocationCity { get; set; }
            public string LocationCountry { get; set; }
            public string DateCreated { get; set; }
        }
    
        // Grab an images meta data direct from file.
        public List<OneImageBluePrint> _OneImageBluePrint(string s)
        {
            // s = Virtual File Path.
    
            // declare a list for items to be added to.
            List<OneImageBluePrint> buildClass = new List<OneImageBluePrint> { };
    
            // Check file is real
            if (File.Exists(HttpContext.Current.Server.MapPath(s)) == true)
            {
                // All ok go for it.
                string _VirtualFilePath = s;
    
                // Set Exif Properties
                string _Title = _returnBitmapMetaData(s).Title;
                string _Subject = _returnBitmapMetaData(s).Subject;
                string[] _Keywords = _returnBitmapMetaData(s).Keywords.ToArray();
                // Set Copy Right
                string _Copyright = _returnBitmapMetaData(s).Copyright;         
    
                // Set Author
                string _Author = null;
                try
                {
                    _Author = _returnBitmapMetaData(s).Author.ToArray()[0].ToString();
                }
                catch{  }
    
                // NOTE: Author is a array.  If want array write like _keywords above.
    
    
                // Set File Info Properties
                FileInfo FI = new FileInfo(HttpContext.Current.Server.MapPath(s));
                string _DateAdded = FI.CreationTimeUtc.ToString();
    
                // Set IPTC properties         
                string _DateCreated = null;
                try
                {
                    _DateCreated = _returnBitmapMetaData(s).GetQuery(@"/app13/irb/8bimiptc/iptc/date created").ToString();
                    string year = _DateCreated.Substring(0, 4);
                    string month = _DateCreated.Substring(4, 2);
                    string day = _DateCreated.Substring(6, 2);
                    _DateCreated = day + "/" + month + "/" + year;
                }
                catch { }
    
                // Set Location Data
                string _locationCountry = null;
                try
                {
                    _locationCountry = _returnBitmapMetaData(s).GetQuery(@"/app13/irb/8bimiptc/iptc/Country\/Primary Location Name").ToString();
                }
                catch { }
                string _LocationCity = null;
                try
                {
                    _LocationCity = _returnBitmapMetaData(s).GetQuery("/app13/irb/8bimiptc/iptc/City").ToString();
                }
                catch { }
                string _LocationPlace = null;
                try
                {
                    _LocationPlace = _returnBitmapMetaData(s).GetQuery(@"/app13/irb/8bimiptc/iptc/Province\/State").ToString();
                }
                catch { }
    
                // Add item to output List
                buildClass.Add(new OneImageBluePrint() { VirtualFilePath = _VirtualFilePath, Title = _Title, Subject = _Subject, Keywords = _Keywords, Author = _Author, Copyright = _Copyright, DateAdded = _DateAdded, LocationPlace = _LocationPlace, LocationCity = _LocationCity, LocationCountry = _locationCountry, DateCreated = _DateCreated });
    
            }
            return buildClass;
        }
    }



    Grab data like this


    image_managment_class IM = new image_managment_class();
    
    var result = (from r in IM._OneImageBluePrint("~/file.jpg")
    
                         select r).Single();
    
    string image_title = result.Titles;
    
    string image_author = result.Author; 

    Hope this helps, any questions just ask.

    Karl.


    PS. Iv'e just done a wiked project in this stuff.  I used the JPG's to store product information in allowing for client to manage there products by images and not DB.  they where much happier. Seemed more tacktile for them.

    Thursday, September 9, 2010 10:59 AM

All replies

  • User1943956234 posted

    Hi,

    This was particularly helpful for developing a comment preview system in ASP.net.

    However, the first code fragment (image_management_class) doesn't close off the filestream when the data has been obtained. This is causing file "in use" problems even after the code has been executed. To fix it, IIS has to be restarted. Is there some way of closing the filestream fs after the comment data has been obtained?

    Thanks

    Monday, October 11, 2010 10:37 PM
  • User-412773370 posted

    where we are supposed to use 

        image_managment_class IM = new image_managment_class();
        string image_title =    IM.returnBitmapMetaData("images\\DSC_0103.jpg").Title;
    
        // Hit intellisense after dot for list of EXIF properties.
    
        // IPTC - little harder.
        string date_created = IM._returnBitmapMetaData("images\\DSC_0103.jpg").GetQuery(@"/app13/irb/8bimiptc/iptc/date created").ToString();
    

    and 

           image_managment_class IM = new image_managment_class();
    
            var result = (from r in IM._OneImageBluePrint("images\\DSC_0103.jpg")
    
                          select r).Single();
    
            string image_title = result.Titles;
    
            string image_author = result.Author; 

     how to call this in my aspx.cs page

    Saturday, April 19, 2014 7:05 AM