Skip to content

Simple and efficient C# .netstandard 2.1 library for detecting dimensions, resolution, mime type and metadata for common image formats

License

Notifications You must be signed in to change notification settings

CodeRanger-com/Coderanger.ImageInfo

Repository files navigation

Coderanger.ImageInfo

Build and Publish Line Coverage Status Branch Coverage Status

Overview

Coderanger.ImageInfo is a very simple cross-platform dotnet core .netstandard2.1 library that enables you to inspect images and return back their dimensions and metadata.

Supported Formats

Coderanger.ImageInfo is able to decode the dimensions and resolution of the following graphic formats:

  • JPEG
  • PNG
  • WEBP
  • BMP (does not support the storage of metadata)
  • GIF

Metadata Extraction

Coderanger.ImageInfo can also extract metadata with the following profiles:

  • EXIF
  • IPTC
  • PNG Text
  • XMP

Metadata is stored as a Dictionary of Metadata Tag Values against a profile.

It should be noted, however, that XMP data is an exception and is currently stored as a single string Tag Value which contains the unparsed XML, so any parsing of the XMP XML needs to be done outside of this library (for the time being).

Usage

This library is very easy to use; with just a single static method called with an image stream, and the response being either NULL or an ImageDetails record containing properties for the various items of image data:

using var imageStream = new FileStream( "image.jpeg", FileMode.Open, FileAccess.Read );
var imageInfo = ImageInfo.Get( imageStream );

if( imageInfo is not null )
{
  // If imageInfo was decodable, an object is returned with metadata properties
  Debug.WriteLine( $"Width = {imageInfo.Width}" );
  Debug.WriteLine( $"Height = {imageInfo.Height}" );
  Debug.WriteLine( $"Horizontal DPI = {imageInfo.HorizontalResolution}" );
  Debug.WriteLine( $"Vertical DPI = {imageInfo.VerticalResolution}" );
  Debug.WriteLine( $"Mime = {imageInfo.MimeType}" );

  // If there is any metadata in the image, the 'Tags' property
  // will contain the info as a dictionary of profile tag lists

  // For example, the following will output the tags in the 'Exif' profile
  if( imageInfo.Metadata?.TryGetValue( MetadataProfileType.Exif, out var tags ) ?? false && tags is not null )
  {
    foreach( var tag in tags )
    {
      if( tag is not null && tag.HasValue )
      {
        if( tag.TryGetValue( out var metadataValue ) && metadataValue is not null )
        {
          Debug.WriteLine( $"{metadataValue.TagName} = {metadataValue.Value}" );
        }
      }
    }
  }
}

How can you help?

Contributions are welcome; even if it is just additional test images for subtypes of an image format so that the test suite can be increased.

Find out more on our how to contribute.

Licence

Coderanger.ImageInfo is licensed under the MIT Licence.