Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Read Photo Taken Date

Read Photo Taken Date

Scheduled Pinned Locked Moved C#
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    S K Y
    wrote on last edited by
    #1

    How can I read the photo taken date(not the created date or modified date). I found on Google that can be do by propertyItems. There’s no date taken property in Property items. Is there any possible way to read the photo taken date??? :confused:

    A S E L A

    L 1 Reply Last reply
    0
    • S S K Y

      How can I read the photo taken date(not the created date or modified date). I found on Google that can be do by propertyItems. There’s no date taken property in Property items. Is there any possible way to read the photo taken date??? :confused:

      A S E L A

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Some file formats (including JPEG) allow for a lot of metadata, and most camera's seem to provide at least some of these pieces of information. Here is some code that may be useful to you; I will not provide support on it though.

      string s=readImageProperty(image, 0x132);	// ASCII
      log("datetime="+s);
      DateTime exposureDate=ParseExposureDate(s, equipModel);
      log("Exposure DateTime = "+DateToString(exposureDate, false, ""));
      

      // reads a specific property from an image, and returns it as a string
      // (whatever format it originally had);
      // returns null when property is not available.
      // typically only works if image got loaded from a JPEG file.
      private static string readImageProperty(Image image, int ID) {
      try {
      PropertyItem pi=image.GetPropertyItem(ID); // DateTime
      if(pi!=null) {
      if(pi.Type==2) { // ASCII
      return ASCIIencoding.GetString(pi.Value, 0, pi.Len-1);
      }
      if(pi.Type==5) { // rational
      byte[] bb=pi.Value;
      uint uNominator=BitConverter.ToUInt32(bb, 0);
      uint uDenominator=BitConverter.ToUInt32(bb, 4);
      if(uDenominator==1) return uNominator.ToString();
      return uNominator.ToString()+"/"+uDenominator.ToString();
      }
      }
      } catch { // catch and ignore all kinds of exceptions, such as "no such property"
      }
      return null;
      }

      // Converts a JPEG datetime property to a standard DateTime;
      // may try several known formats, and returns DateTime.MinValue on failure.
      // REMARK: if necessary, make the format candidates depend on
      // (part of) the equipment model
      private static DateTime ParseExposureDate(string exposure, string equipModel) {
      DateTime dt=DateTime.MinValue;
      bool OK=false;
      // repeat the next IF block, with different arguments, for each
      // possible DateTime format (or each camera)
      if(!OK) {
      // NIKON D70s gives: "2007:04:07 09:02:00"
      // Canon PowerShot A620 gives: "2006:09:25 18:43:33"
      // Olympus C460 gives: "2006:03:12 15:49:54"
      // Olympus FE200 gives: "2007:03:17 01:18:30"
      OK=DateTime.TryParseExact(exposure, "yyyy:MM:dd HH:mm:ss",
      null, DateTimeStyles.None, out dt);
      }
      log("ParseExposureDate: \""+exposure+"\" >>> "+dt);
      return dt;
      }

      :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your que

      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Some file formats (including JPEG) allow for a lot of metadata, and most camera's seem to provide at least some of these pieces of information. Here is some code that may be useful to you; I will not provide support on it though.

        string s=readImageProperty(image, 0x132);	// ASCII
        log("datetime="+s);
        DateTime exposureDate=ParseExposureDate(s, equipModel);
        log("Exposure DateTime = "+DateToString(exposureDate, false, ""));
        

        // reads a specific property from an image, and returns it as a string
        // (whatever format it originally had);
        // returns null when property is not available.
        // typically only works if image got loaded from a JPEG file.
        private static string readImageProperty(Image image, int ID) {
        try {
        PropertyItem pi=image.GetPropertyItem(ID); // DateTime
        if(pi!=null) {
        if(pi.Type==2) { // ASCII
        return ASCIIencoding.GetString(pi.Value, 0, pi.Len-1);
        }
        if(pi.Type==5) { // rational
        byte[] bb=pi.Value;
        uint uNominator=BitConverter.ToUInt32(bb, 0);
        uint uDenominator=BitConverter.ToUInt32(bb, 4);
        if(uDenominator==1) return uNominator.ToString();
        return uNominator.ToString()+"/"+uDenominator.ToString();
        }
        }
        } catch { // catch and ignore all kinds of exceptions, such as "no such property"
        }
        return null;
        }

        // Converts a JPEG datetime property to a standard DateTime;
        // may try several known formats, and returns DateTime.MinValue on failure.
        // REMARK: if necessary, make the format candidates depend on
        // (part of) the equipment model
        private static DateTime ParseExposureDate(string exposure, string equipModel) {
        DateTime dt=DateTime.MinValue;
        bool OK=false;
        // repeat the next IF block, with different arguments, for each
        // possible DateTime format (or each camera)
        if(!OK) {
        // NIKON D70s gives: "2007:04:07 09:02:00"
        // Canon PowerShot A620 gives: "2006:09:25 18:43:33"
        // Olympus C460 gives: "2006:03:12 15:49:54"
        // Olympus FE200 gives: "2007:03:17 01:18:30"
        OK=DateTime.TryParseExact(exposure, "yyyy:MM:dd HH:mm:ss",
        null, DateTimeStyles.None, out dt);
        }
        log("ParseExposureDate: \""+exposure+"\" >>> "+dt);
        return dt;
        }

        :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your que

        S Offline
        S Offline
        S K Y
        wrote on last edited by
        #3

        [Message Deleted]

        L 1 Reply Last reply
        0
        • S S K Y

          [Message Deleted]

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          browsing the documentation for 10 seconds would reveal the namespace. I am not in the spoon feeding business, I try to provide added value. :~

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups