Convert all Images in any Format (PNG, BMP etc) in Word DOCX Document to .jpeg Extension C# .NET | PNG to JPG

Is there any possibility of updating the image extension .png to .jpg using aspose word in docx file

@tahir.manzoor

@sreddyD,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document containing the images with PNG or other extensions
  • Your expected DOCX file showing the desired output i.e. DOCX containing images with .jpg extensions only. You can create this document by using MS Word or any other editor. Please also list the complete steps that you performed in MS Word to create the expected document on your end

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same by using Aspose.Words for .NET API.

@awais.hafeez
Attached the docx file and points, expecting a quick response from you.Test image converting png to jpg or jpeg.zip (21.3 KB)

@sreddyD,

Please try the following C# code of Aspose.Words for .NET API that will convert all Images (in any format such as PNG, BMP etc) in Word DOCX document to .jpeg extension:

Document doc = new Document("C:\\temp\\Test image converting png to jpg or jpeg\\Test image converting png to jpg or jpeg.docx");
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.HasImage)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            ShapeRenderer shapeRenderer = shape.GetShapeRenderer();
            shapeRenderer.Save(stream, new ImageSaveOptions(SaveFormat.Jpeg));
            stream.Position = 0;
            shape.ImageData.SetImage(stream);
        }
    }
}
doc.Save("C:\\temp\\Test image converting png to jpg or jpeg\\20.9.docx");

@awais.hafeez
Do you have an option for converting it into .jpg instead of jpeg extension.

@sreddyD,

We are checking this scenario and will get back to you soon.

@sreddyD,

I am afraid, there is no way that you can use to convert .jpeg to .jpg extension. Aspose.Words always writes a full name extension i.e. JPEG. There are actually no differences between the JPG and JPEG formats. The only difference is the number of characters used in extension.

If we can help you with anything else, please feel free to ask.