I'm developing a web application where a photographer does upload of an image. After copying the image, I'm going to scan all the metadata of the image in order to capture some information from the directories (Exif, IPTC, XMP, JFIF, PNG, BMP, href="https://github.com/drewnoakes/metadata-extractor"> Metadata Extractor . Okay, I learned to use it. First I need to get the directories:
IEnumerable<MetadataExtractor.Directory> directories = iptcReader.ReturnMetadataFile(Path.Combine(StorageRootPOOL, imagePool.File_name[i]));
Here I already have all the directories of my image, after that I need to declare variables of each directory that I need to capture in my case:
var IPTCDirectory = directories.OfType<IptcDirectory>().FirstOrDefault();
var JPEGDirectory = directories.OfType<JpegDirectory>().FirstOrDefault();
var JFIFDirectory = directories.OfType<JfifDirectory>().FirstOrDefault();
var EXIFDirectory = directories.OfType<ExifIfd0Directory>().FirstOrDefault();
var EXIFSubDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
All directory values are NOT NULL even though there is no directory in the image, the problem is in tags that exist within them. To capture the tags within the directories I use the following code:
imageFileInfo.Keywords = IPTCDirectory.GetDescription(IptcDirectory.TagKeywords);
imageFileInfo
is the class where I will be saving all the data. My problem is, if for example there is no TAG IptcDirectory.TagKeywords
I will get a NullException, so nothing out of the ordinary, however if I will prevent this error with an if for example:
if (IPTCDirectory.GetDescription(IptcDirectory.TagKeywords) == null)
{
}
I get another NullException
, there's the problem, I wanted to set a default value for tags that do not exist, for example:
if (IPTCDirectory.GetDescription(IptcDirectory.TagKeywords) == null)
{
imageFileInfo.Keywords = "valor padrão";
}
Remembering that I do not use the following code, because I need to know which tags are null and not what directories, since the directories always exist, but some tags do not :
if (EXIFDirectory != null && ExifIfd0Directory != null) {
//Os diretórios nunca são nulos e sim as tags, então esse exemplo não iria se aplica ao meu problema.
}