Extracting image from WMF/EMF

Hi!

For example we have the shape in a document with the following properties:
  • shapeType=Image
  • hasImage=true
  • imageType=Emf
  • hasChart=false
  • fileExtension=.emf
  • isOle=false
  • shape size(px)=467.6967716535433 x 244.8
  • image size(pt)=1534.3360613334207 x 804.1978712566225
  • image size(px)=1534 x 804
  • resolution=71.9842300415039 x 71.98228454589844
We need to render the image to a file and we are using the following code:
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setResolution(shape.getImageData().getImageSize().getHorizontalResolution()); // or 96.0 by default
options.setUseHighQualityRendering(true);
options.setUseAntiAliasing(true);
shape.getShapeRenderer().save("...", options);

As a result we get the image with size 470x245px.

Questions:
  1. How can we detect the source format of (bitmap) image in a WMF/EMF (PNG, JPEG, or vector graphic)?
  2. How can we render the source image with original size (and resolution?)?

--
Best regards,
Peter Titov
Hi Peter,

Thanks for your inquiry.

1 - Please use the Shape.ImageData.ImageType property.

2 - Please try running the following code snippet to get both the original and 'as displayed in MS Word' images.
Document doc = new Document("C:\\Temp\\input.doc");
Shape shape =  (Shape) doc.selectNodes("//Shape").get(0);
// to get the cropped/displayed shape
shape.getShapeRenderer().save("C:\\Temp\\outCropped.png", new ImageSaveOptions(SaveFormat.PNG));
//to get the actual image
shape.getImageData().save("C:\\Temp\\outOriginal.png");

I hope, this helps.

Best regards,

Hi Awais,


1. Shape.ImageData.ImageType = EMF (see my example) - it means nothing to me, I need to know of type of raw (source) data - bitmap (e.g. PNG) or vector graphics. Is it possible?

2. I need to get the image in form before inserted in the document with source resolution and size.
shape.getImageData().save(…) saves EMF data, not actual image.
shape.getShapeRenderer().save(…) saves cropped image.

Peter Titov

Hi Peter,

1 - I think, you can meet this requirement by using Aspose.Imaging for Java. Please see the following method:

com.aspose.imaging.Image.getFileFormat()

You can pass String or InputStream as input parameter to this method. This method detects the image format regardless of the file extension. Hope, this helps.

2 - Can you please elaborate what do you mean when saying that you need to get the image in form before inserted in the document with source resolution and size? Do any of the following methods work for you?

shape.getShapeRenderer().save
shape.getImageData().save

If not, what method would you like to see in Aspose.Words API? Please provide complete signature of expected method i.e. return type and input parameters etc.

Best regards,

Hi Awais,


2.
shape.getImageData().save(…) saves the EMF data, but I need the image.
shape.getShapeRenderer().save(…) saves the cropped image, but I need the full size image.

Let me show you an example of existing shape again:
  • shapeType=Image
  • hasImage=true
  • imageType=Emf
  • hasChart=false
  • fileExtension=.emf
  • isOle=false
  • shape size(px)=467.6967716535433 x 244.8
  • image size(pt)=1534.3360613334207 x 804.1978712566225
  • image size(px)=1534 x 804
  • resolution=71.9842300415039 x 71.98228454589844
So I need to get the image with size 1534x804 px, but using shape.getShapeRenderer().save(…) I get the image with size 468x245 px (shape size in the document).

Peter Titov

Hi Peter,


Please open a blank document in MS Word
Insert a picture
Resize the picture using MS Word
Save the file to Docx format
Now re-open DOCX with MS Word
Right click on picture and choose “Save as Picture”. Save it
What size do you get in this case?

I found that MS Word 2016 generated picture has a lesser size same as ImageData.Save returns. This seems an expected behavior as the original size information seems not present in Document itself.

Best regards,