How to get cropped image?

Team,


I am using Aspose latest version for document conversion. I have attached the document containing cropped image with this. Here is my code to get image.

Document doc = new Document("./crop.doc");
Shape node = (Shape) doc.selectNodes("//Shape").get(0);
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
node.getShapeRenderer().save(imageStream, new ImageSaveOptions(SaveFormat.PNG));
byte[] imageBytes = node.getImageData().getImageBytes();
writeImage("./image.png", imageBytes);

Original image is saved instead of cropped image.
Am i doing anything wrong ?

It is working fine for .DOCX format.

Help me !

Hi Anbu,

Thanks for your inquiry.

Anbu2Win:

Original image is saved instead of cropped image.

It would be great if you please share some more detail about your query. We will then provide you more information on this along with code.

The Shape.getShapeRenderer method creates and returns an object that can be used to render this shape into an image. Please also check ShapeRenderer. renderToScale and ShapeRenderer. renderToSize method from here:

Hi Anbu,

Thanks for your inquiry. The crop tool in Microsoft Word can only trim/remove unwanted portions of pictures to get just the look that you want to make. Cropping doesn’t actually modify the original picture; instead, it just hides a part of a picture.

In your case, the image is actually stored inside a Shape node and you can always access the actual (full) image using the ToByteArray , ToImage or Save methods. If the image is stored inside the shape, you can also directly access it using the ImageBytes property.

However, please note that the cropping effect is made only on the Shape node but not on the actual image. Therefore, if you want to render the cropped Shape, please use ShapeRenderer class.

Best regards,

Hi,

I have attached screenshot with this post. Hope now you can understand what my problem is.
Using api i am trying to save image locally, original image is saved instead of cropped image.

Thanks

Hi ,


Thanks for the additional information. Please try run the following code snippet to get both the original and the cropped images:
Document doc = new Document(“C:\Temp\crop.doc”);
Shape shape = (Shape) doc.selectNodes("//Shape").get(0);
// to get the cropped 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,

Thanks Awais Hafeez. It is working now.