Get Resolution and Dimensions of Images

[ ]

This topic explains how to use the operator classes in the Aspose.PDF namespace which provide the capability to get resolution and dimension information about images without having to extract them.

There are different ways of achieving this. This article explains how to use an arraylist and image placement classes.

  1. First, load the source PDF file (with images).
  2. Then create an ArrayList object to hold the names of any images in the document.
  3. Get the images using the Page.Resources.Images property.
  4. Create a stack object to hold the image’s graphics state and use it to keep track of different image states.
  5. Create a ConcatenateMatrix object which defines current transformation. It also supports scaling, rotating, and skewing any content. It concatenates the new matrix with previous one. Please note that we cannot define the transformation from scratch but only modify the existing transformation.
  6. Because we can modify the matrix with ConcatenateMatrix, we may also need to revert back to the original image state. Use GSave and GRestore operators. These operators are paired so they should be called together. For example, if you do some graphics work with complex transformations and finally return transformations back to initial state, the approach will be something like this:
// Draw some text

ConcatenateMatrix  // rotate contents after the operator

// Some graphics work

ConcatenateMatrix // scale (with previous rotation) contents after the operator

// Some other graphics work


// Draw some text

As a result, text is drawn in regular form but some transformations are performed between the text operators. In order to display the image or to draw form objects and images, we need to use the Do operator.

We also have a class named XImage which provides two properties,l Width and Height, which can be used to get image dimensions.

  1. Perform some calculations to compute the image resolution.
  2. Display the information in a Command Prompt along with the image name.

The following code snippet shows you how to get an image’s dimensions and resolution without extracting the image from the PDF document.

// For complete examples and data files, please go to
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Images();

// Load the source PDF file
Document doc = new Document(dataDir+ "ImageInformation.pdf");

// Define the default resolution for image
int defaultResolution = 72;
System.Collections.Stack graphicsState = new System.Collections.Stack();
// Define array list object which will hold image names
System.Collections.ArrayList imageNames = new System.Collections.ArrayList(doc.Pages[1].Resources.Images.Names);
// Insert an object to stack
graphicsState.Push(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 0, 0));

// Get all the operators on first page of document
foreach (Operator op in doc.Pages[1].Contents)
    // Use GSave/GRestore operators to revert the transformations back to previously set
    Aspose.Pdf.Operators.GSave opSaveState = op as Aspose.Pdf.Operators.GSave;
    Aspose.Pdf.Operators.GRestore opRestoreState = op as Aspose.Pdf.Operators.GRestore;
    // Instantiate ConcatenateMatrix object as it defines current transformation matrix.
    Aspose.Pdf.Operators.ConcatenateMatrix opCtm = op as Aspose.Pdf.Operators.ConcatenateMatrix;
    // Create Do operator which draws objects from resources. It draws Form objects and Image objects
    Aspose.Pdf.Operators.Do opDo = op as Aspose.Pdf.Operators.Do;

    if (opSaveState != null)
        // Save previous state and push current state to the top of the stack
    else if (opRestoreState != null)
        // Throw away current state and restore previous one
    else if (opCtm != null)
        System.Drawing.Drawing2D.Matrix cm = new System.Drawing.Drawing2D.Matrix(

        // Multiply current matrix with the state matrix

    else if (opDo != null)
        // In case this is an image drawing operator
        if (imageNames.Contains(opDo.Name))
            System.Drawing.Drawing2D.Matrix lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek();
            // Create XImage object to hold images of first pdf page
            XImage image = doc.Pages[1].Resources.Images[opDo.Name];

            // Get image dimensions
            double scaledWidth = Math.Sqrt(Math.Pow(lastCTM.Elements[0], 2) + Math.Pow(lastCTM.Elements[1], 2));
            double scaledHeight = Math.Sqrt(Math.Pow(lastCTM.Elements[2], 2) + Math.Pow(lastCTM.Elements[3], 2));
            // Get Height and Width information of image
            double originalWidth = image.Width;
            double originalHeight = image.Height;

            // Compute resolution based on above information
            double resHorizontal = originalWidth * defaultResolution / scaledWidth;
            double resVertical = originalHeight * defaultResolution / scaledHeight;

            // Display Dimension and Resolution information of each image
                    string.Format(dataDir + "image {0} ({1:.##}:{2:.##}): res {3:.##} x {4:.##}",
                                 opDo.Name, scaledWidth, scaledHeight, resHorizontal,