Jeroen, a friend of mine was working on a new feature that required to get the original creation date of a file. We teamed up and took the bull by its horns.
Click me if you’re just interested in the code
EXIF
Since we wanted to get the original created date of a picture, I knew we had to look for the EXIF Data of the picture.
EXIF stands for Exchangeable Image File Format. Every time you take a picture with your digital camera or phone, a file (typically a JPEG) is written to your device’s storage. In addition to all the bits dedicated to the actual picture, it records a considerable amount of supplemental metadata as well. This can include date, time, camera settings, and possible copyright information. You can also add further metadata to EXIF, such as through photo processing software.
A quick google search gave me the following nugget library:
Install-Package MetadataExtractor
By using the MetadataExtractor we could get the EXIF data out of the picture when it’s being uploaded.
BE AWARE!!! Some pictures might not have the EXIF data that we want. This all depends on the device that the picture was taken with. So make sure to check the EXIF data of your picture when you are testing it. A simple way is to take a picture with your phone and upload it to your laptop/desktop.
Here is a cool website that allows you to check the EXIF data of your picture:
http://exif.regex.info/exif.cgi
The Code
Install the nugget first: Install-Package MetadataExtractor
Here is an example of uploading a file in a .NET Core controller
var originalDate = exifSubDirectory?.
GetDescription(ExifDirectoryBase.TagDateTimeOriginal);
As you can see in the snippet that I provided, we are asking for the TagDateTimeOriginal tag. There’s a lot more information we can pull from the image.
Happy coding! 🎉