Get Dimension of an image

Can we get dimensions (width and height) of an image using aspose.word or any other aspose library? Code should run in linux and windows.

@trizetto

Cause

You can retrieve the dimensions (width and height) of an image using Aspose.Words. The library provides a class called ImageSize that contains information about the image’s size and resolution.

Solution

To get the dimensions of an image, you can insert the image into a document using the DocumentBuilder and then access the ImageData property of the shape that contains the image. The ImageData property provides an ImageSize object, which includes methods to get the width and height in both pixels and points.

Code

Here’s a simple example in Java:

import com.aspose.words.*;

public class ImageDimensionExample {
    public static void main(String[] args) throws Exception {
        // Create a new document and a DocumentBuilder
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert an image into the document
        Shape shape = builder.insertImage("path/to/your/image.jpg");
        
        // Get the image size
        ImageSize imageSize = shape.getImageData().getImageSize();
        
        // Output the dimensions
        System.out.println("Width in pixels: " + imageSize.getWidthPixels());
        System.out.println("Height in pixels: " + imageSize.getHeightPixels());
        System.out.println("Width in points: " + imageSize.getWidthPoints());
        System.out.println("Height in points: " + imageSize.getHeightPoints());
    }
}

Next Steps

  1. Replace "path/to/your/image.jpg" with the actual path to your image file.
  2. Run the code on both Linux and Windows environments to verify compatibility.
  3. If you need further assistance or have additional questions, please ask!

This should effectively help you retrieve the dimensions of an image using Aspose.Words.