Document Solutions for Imaging
Features / Work with Exif Metadata
In This Topic
    Work with Exif Metadata
    In This Topic

    The DsImaging library supports the extraction and modification of Exif metadata from various image formats, such as JPEG, PNG or TIFF files. With DsImaging, a developer can extract all Exif metadata from the images as mentioned in the Exif specifications sheet, such as the shutter speed, time it was taken, focal length, light value, use of flash, title, date, creator, copyright, description location (GPS data) etc..

    DsImaging provides all the Exif metadata of an image in the ExifProfile class which is available in GrapeCity.Documents.Imaging.Exif namespace. The library also provides ExifProfile property of the GcBitmap class through which Exif metadata of the images can be accessed. The ExifProfile class mainly includes two methods, namely GetTags and GetValues. The GetTags method returns an array of all known tags in the profile. On the other hand, the GetValues method returns a list of all known tags in the profile along with their corresponding values. These tags are represented by the ExifTag enumeration and values are represented by the ExifValue class. These values can also be accessed through special properties such as OrientationResolutionUnitLensModel, etc. provided by the ExifProfile class. The class also caters the unknown tags using the UnknownTags property which gets a list of values for the unknown tags. Moreover, if required, you can save the Exif metadata to a stream or a byte array using SaveToStream and ToByteArray methods of the ExifProfile class respectively and also load the Exif metadata from a stream or a byte array using Load method of the ExifProfile class.

    Image of fire breakout with Exif metadata mentioned by its side

    To extract and modify the EXIF metadata of an image:

    1. Initialize the GcBitmap class.
    2. Create an instance of ExifProfile class and get the instance with the Exif metadata of the image using the ExifProfile property.
    3. Get all the known tags values using the GetValues method of ExifProfile class.
    4. Access all the known tags of the profile using the GetTags method of ExifProfile class.
    5. Initialize an instance of the TextLayout class and add all the known tags and values to the TextLayout object.
    6. Render the EXIF metadata of the image along with the image using the DrawTextLayout and DrawImage methods respectively.
      C#
      Copy Code
          //Image path
          var imgPath = Path.Combine("Resources", "Images", "fire.jpg");
      
          //Initialize GcBitmap and create bitmap graphics
          GcBitmap origbmp = new GcBitmap(imgPath);
      
          //Get all the known tags values
          ExifProfile ep = origbmp.ExifProfile;
          List<KeyValuePair<ExifTag, ExifValue>> knownTagsValues = ep.GetValues();
          
          //Create TextLayout used to show EXIF metadata of the image
          TextLayout tl = new TextLayout();
          if (knownTagsValues.Count > 0)
          {
              tl.Append("Known tags(" + knownTagsValues.Count.ToString() + "): \r\n");
              tl.AppendLine();
      
              //Add known tags values to the textlayout
              foreach (KeyValuePair<ExifTag, ExifValue> tag in knownTagsValues)
                  tl.AppendLine(tag.Key + "\t" + tag.Value);
          }
          else
              tl.Append("No known tags");
      
          //Render the created TextLayout and the original image on the output image
          GcBitmap targetBmp = new GcBitmap(700, 850, true);
          GcBitmapGraphics g = targetBmp.CreateGraphics(Color.White);
          using (var img = Image.FromFile(imgPath))
              g.DrawImage(img, new RectangleF(20, 30, 200, 200), null, 
                  ImageAlign.ScaleImage);
          g.DrawTextLayout(tl, new PointF(260, 30));
      
          //Save the image
          targetBmp.SaveAsJpeg("ExifMetadata.jpg");
      }       
      

    Back to Top

    For more information about working with EXIF metadata using DsImaging, see DsImaging sample browser.