Search code examples
asp.net-mvcimage-processingc#-4.0exif

MVC 5 Get Exif data for Camera make


I am trying to Get Exif data for Camera make, ISO speed etc. in a file upload. I can get some tags (see below) but need some guidance on extracting items from the Exif directories. Any suggestions please.

            IEnumerable<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(strFileName);
        foreach (var directory in directories)
            foreach (var tag in directory.Tags)
                System.Diagnostics.Debug.WriteLine(string.Format("Directory " + $"{directory.Name} - {tag.Name} = {tag.Description}"));

        var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
        var dateTime = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTime);
        System.Diagnostics.Debug.WriteLine(string.Format("dateTime " + dateTime));

        //
        Image img = Image.FromFile(strFileName);
        ImageFormat format = img.RawFormat;
        System.Diagnostics.Debug.WriteLine("Image Type : " + format.ToString());
        System.Diagnostics.Debug.WriteLine("Image width : " + img.Width);
        System.Diagnostics.Debug.WriteLine("Image height : " + img.Height);
        System.Diagnostics.Debug.WriteLine("Image resolution : " + (img.VerticalResolution * img.HorizontalResolution));
        System.Diagnostics.Debug.WriteLine("Image Pixel depth : " + Image.GetPixelFormatSize(img.PixelFormat));


        PropertyItem[] propItems = img.PropertyItems;
        int count = 0;
        ArrayList arrayList = new ArrayList();
        foreach (PropertyItem item in propItems)
        {
            arrayList.Add("Property Item " + count.ToString());
            arrayList.Add("iD: 0x" + item.Id.ToString("x"));
            System.Diagnostics.Debug.WriteLine("PropertyItem item in propItems: " + item.Id.ToString("Name"));
            count++;
        }

        ASCIIEncoding encodings = new ASCIIEncoding();
        try
        {
            string make = encodings.GetString(propItems[1].Value);
            arrayList.Add("The equipment make is " + make.ToString() + ".");
        }
        catch
        {
            arrayList.Add("no Meta Data Found");
        }

        ViewBag.listFromArray = arrayList;
        return View(await db.ReadExifs.ToListAsync());
    }

Two loops I know, messy but gives some output :

Directory JPEG - Compression Type = Baseline
Directory JPEG - Data Precision = 8 bits
Directory JPEG - Image Height = 376 pixels
Directory JPEG - Image Width = 596 pixels
Directory JPEG - Number of Components = 3
Directory JPEG - Component 1 = Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
Directory JPEG - Component 2 = Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
Directory JPEG - Component 3 = Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
Directory JFIF - Version = 1.1
Directory JFIF - Resolution Units = inch
Directory JFIF - X Resolution = 120 dots
Directory JFIF - Y Resolution = 120 dots
Directory JFIF - Thumbnail Width Pixels = 0
Directory JFIF - Thumbnail Height Pixels = 0
Directory File - File Name = FakeFoto03_large.Jpg
Directory File - File Size = 66574 bytes
Directory File - File Modified Date = Tue Jan 03 00:02:00 +00:00 2017
Image Type : [ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]
Image width : 596
Image height : 376
Image resolution : 14400
Image Pixel depth : 24

Thanks. Y.


Solution

  • Solved. This block:

                ArrayList arrayList = new ArrayList();
    
            IEnumerable<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(strFileName);
            foreach (var directory in directories)
                foreach (var tag in directory.Tags)
                    //   System.Diagnostics.Debug.WriteLine(string.Format("Directory " + $"{directory.Name} - {tag.Name} = {tag.Description}"));
              arrayList.Add($"{tag.Name} = {tag.Description}");
    
            ViewBag.listFromArray = arrayList;
            return View(await db.ReadExifs.ToListAsync());
    

    This produces (in the case of the Photo used as source) 120 exif tags. Sample: White Balance Mode = Auto white balance

    Digital Zoom Ratio = 1

    Focal Length 35 = 28 mm

    Scene Capture Type = Standard

    Gain Control = Low gain up

    Contrast = None