How to Get DPI of Image in C#

Learn how to get dpi of image in c# with practical examples, diagrams, and best practices. Covers c#, dpi development techniques with visual explanations.

How to Get DPI of an Image in C#

Hero image for How to Get DPI of Image in C#

Learn various methods to programmatically retrieve the DPI (Dots Per Inch) of an image file using C#, covering GDI+ and modern alternatives.

Understanding an image's DPI (Dots Per Inch) is crucial for tasks like printing, display scaling, and ensuring visual quality. In C#, you can access this information using built-in libraries. This article explores different approaches, primarily focusing on the System.Drawing namespace (GDI+) and discussing considerations for its use in modern applications.

Using System.Drawing.Image for DPI Retrieval

The most straightforward way to get an image's DPI in C# is by using the System.Drawing.Image class, which is part of GDI+. This class provides properties to directly access the horizontal and vertical resolution (DPI) of an image. While System.Drawing is a mature library, it's important to note its platform dependency and potential limitations in modern .NET Core/.NET 5+ applications, especially in non-Windows environments.

using System.Drawing;

public class ImageDpiReader
{
    public static (float DpiX, float DpiY) GetImageDpi(string imagePath)
    {
        try
        {
            using (Image image = Image.FromFile(imagePath))
            {
                float dpiX = image.HorizontalResolution;
                float dpiY = image.VerticalResolution;
                return (dpiX, dpiY);
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine($"Error: Image file not found at {imagePath}");
            return (0, 0);
        }
        catch (OutOfMemoryException)
        {
            Console.WriteLine($"Error: Not enough memory to open or process the image at {imagePath}. It might be corrupted or an unsupported format.");
            return (0, 0);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
            return (0, 0);
        }
    }

    public static void Main(string[] args)
    {
        string imageFilePath = "path/to/your/image.jpg"; // Replace with your image path
        (float dpiX, float dpiY) = GetImageDpi(imageFilePath);

        if (dpiX > 0 && dpiY > 0)
        {
            Console.WriteLine($"Image DPI: Horizontal = {dpiX}, Vertical = {dpiY}");
        }
        else
        {
            Console.WriteLine("Could not retrieve image DPI.");
        }
    }
}

Understanding Image DPI Retrieval Process

The process of retrieving DPI involves loading the image into memory and then accessing its metadata. Most image formats (like JPEG, TIFF, PNG) store DPI information within their headers. The System.Drawing.Image.FromFile() method handles the parsing of these headers to expose the HorizontalResolution and VerticalResolution properties. If an image does not explicitly store DPI (e.g., some web images), these properties might default to 96 DPI, which is a common screen resolution standard.

flowchart TD
    A[Start: Provide Image Path] --> B{Load Image File}
    B -- Success --> C[Create System.Drawing.Image Object]
    C --> D[Access HorizontalResolution Property]
    C --> E[Access VerticalResolution Property]
    D --> F[Get DPI_X]
    E --> G[Get DPI_Y]
    F & G --> H[Return (DPI_X, DPI_Y)]
    B -- Failure --> I[Handle File Not Found/Corrupted Image]
    I --> J[Return (0, 0) or Error]
    H --> K[End]
    J --> K[End]

Flowchart of the C# GDI+ Image DPI Retrieval Process

Alternative: Using Third-Party Libraries for Cross-Platform Compatibility

For robust, cross-platform image processing in .NET Core and .NET 5+, it's often recommended to use third-party libraries that abstract away platform-specific GDI+ dependencies. Libraries like ImageSharp or Magick.NET (a .NET wrapper for ImageMagick) offer comprehensive image manipulation capabilities, including DPI retrieval, and are designed for modern .NET applications across various operating systems.