1

I have images(Jpg, gif ,png etc..) local hard disk. What I need to read all file information such as location details latitude longitudes or location name and photos created date each images. Please help me how do I read location details and date.

This is my C# code it not display location details or created date

 string directory = (@"C:\Photos");
 var AllImages = Directory.GetFiles(directory, "*.jpg",
                                   SearchOption.AllDirectories)
                         .Select(Image.FromFile).ToList()

This is one of my photo example

.enter image description here

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Rob
  • 193
  • 5
  • 18

1 Answers1

4

The keyword you're looking for is image metadata, or EXIF data. You can read the raw data from the Image.PropertyItems property and parse it manually, or you can use an external library to do the work for you.

I'd recommend ExifLib - it's a very basic yet capable library for reading EXIF data. Here's an example:

// Instantiate the reader
using (ExifReader reader = new ExifReader(@"C:\temp\testImage.jpg"))
{
    // Extract the tag data using the ExifTags enumeration
    DateTime datePictureTaken;
    if (reader.GetTagValue<DateTime>(ExifTags.DateTimeDigitized, out datePictureTaken))
    {
        // Do whatever is required with the extracted information
        MessageBox.Show(this, string.Format("The picture was taken on {0}", 
           datePictureTaken), "Image information", MessageBoxButtons.OK);
    }
}

Metadata Extractor dotnet

As mentioned by others, an alternative is using the dotnet version of metadata extractor.

Example to retrieve timestamp:

var directories = ImageMetadataReader.ReadMetadata(fileStream);
                var directory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
                return directory?.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal);
Ciaran Gallagher
  • 3,895
  • 9
  • 53
  • 97
Gediminas Masaitis
  • 3,172
  • 14
  • 35
  • I am using visual studio 2015, do I install packages ExifLib. Also I need read all images folder. eg C:\Photos – Rob Mar 16 '16 at 00:00
  • 1
    Also check out [metadata-extractor](https://github.com/drewnoakes/metadata-extractor-dotnet). – Drew Noakes Oct 03 '16 at 21:24
  • In case someone's coming here looking for a Unity compatible version: Metadata-Extractor seems to use .NET language features not available in the Unity .NET4.0 install of e.g. Unity 2019.3.0f6. – Philipp Lenssen Oct 11 '20 at 08:28