Use Shape Renderer to Extract Images with Formatting from Word DOCX Document (C# .NET) & Save to PNG

Hi,

I’m currently using Aspose version 19.12, and I found ShapeRenderer is the one I need to extract image from specific Shape, but I get different output image attached.
ImageExtraction.jpg (225.6 KB)

I used the following code snippets below:

public static void RenderShapeToGraphics(AsposeWords.Document doc, string imagesDir)
    {
        AsposeWords.NodeCollection shapes = doc.GetChildNodes(AsposeWords.NodeType.Shape, true);

        foreach (Shape shape in shapes)
        {
            if (shape.HasImage)
            {

                ShapeRenderer r = shape.GetShapeRenderer();
                Size shapeSizeInPixels = r.GetSizeInPixels(1.0f, 96.0f);

                using (Bitmap image = new Bitmap(shapeSizeInPixels.Width, shapeSizeInPixels.Height))
                {
                    using (Graphics gr = Graphics.FromImage(image))
                    {
                        gr.Clear(shape.Document.PageColor);

                        r.RenderToSize(gr, 0, 0, shapeSizeInPixels.Width, shapeSizeInPixels.Height);
                    }
                    string imageFilename = System.IO.Path.Combine(imagesDir, $"img_{shape.Name}.png");
                    image.Save(imageFilename, ImageFormat.Png);
                }
            }
        }
    }

private static void ExtractUsingShapeRender(AsposeWords.Document doc, string extractionDir)
    {
        // https://docs.aspose.com/display/wordsnet/Rendering+Shapes+Separately+from+a+Document
        AsposeWords.NodeCollection shapes = doc.GetChildNodes(AsposeWords.NodeType.Shape, true);
        int imageIndex = 0;

        string imagesDir = System.IO.Path.Combine(extractionDir, "SaveWithShapeRenderer");
        // The folder specified needs to exist and should be empty
        if (System.IO.Directory.Exists(imagesDir))
            System.IO.Directory.Delete(imagesDir, true);

        System.IO.Directory.CreateDirectory(imagesDir);

        GraphicsQualityOptions qualityOptions = new AsposeWords.Saving.GraphicsQualityOptions
        {
            SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias,
            TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit,
            CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver,
            CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality,
            InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High,
            StringFormat = System.Drawing.StringFormat.GenericTypographic
        };

        foreach (AsposeWords.Drawing.Shape shape in shapes)
        {
            if (shape.HasImage)
            {
                // Define custom options which control how the image is rendered. Render the shape to the vector format JPG.
                AsposeWords.Saving.ImageSaveOptions imageOptions = new AsposeWords.Saving.ImageSaveOptions(AsposeWords.SaveFormat.Jpeg)
                {
                    Scale = 1f,
                    JpegQuality = 100,
                    UseHighQualityRendering = true,
                    DmlEffectsRenderingMode = AsposeWords.Saving.DmlEffectsRenderingMode.Fine,
                    DmlRenderingMode = AsposeWords.Saving.DmlRenderingMode.Fallback,
                    UseAntiAliasing = true,
                    GraphicsQualityOptions = qualityOptions,
                }; 

                imageIndex++;
                AsposeWords.Rendering.ShapeRenderer renderer = new AsposeWords.Rendering.ShapeRenderer(shape);
                string imageFilename = System.IO.Path.Combine(imagesDir, $"img{imageIndex}.jpg");
                renderer.Save(imageFilename, imageOptions);

                // Define custom options which control how the image is rendered. Render the shape to the vector format PNG.
                imageOptions = new AsposeWords.Saving.ImageSaveOptions(AsposeWords.SaveFormat.Png)
                {
                    Scale = 1f,
                    UseHighQualityRendering = true,
                    DmlEffectsRenderingMode = AsposeWords.Saving.DmlEffectsRenderingMode.Fine,
                    DmlRenderingMode = AsposeWords.Saving.DmlRenderingMode.Fallback,
                    UseAntiAliasing = true,
                    GraphicsQualityOptions = qualityOptions,
                };
                
                renderer = new AsposeWords.Rendering.ShapeRenderer(shape);
                imageFilename = System.IO.Path.Combine(imagesDir, $"img{imageIndex}.png");
                renderer.Save(imageFilename, imageOptions);
            }
        }
    }

private static void ExtractUsingHtmlOptions(AsposeWords.Document doc, string extractionDir)
    {
        string imagesDir = System.IO.Path.Combine(extractionDir, "SaveHtmlWithOptions");
        // The folder specified needs to exist and should be empty
        if (System.IO.Directory.Exists(imagesDir))
            System.IO.Directory.Delete(imagesDir, true);

        System.IO.Directory.CreateDirectory(imagesDir);

        // https://reference.aspose.com/words/net/aspose.words.saving/htmlsaveoptions/properties/imagesfolder
        //// Set an option to export form fields as plain text, not as HTML input elements
        AsposeWords.Saving.HtmlSaveOptions options = new AsposeWords.Saving.HtmlSaveOptions(AsposeWords.SaveFormat.Html);
        options.ExportTextInputFormFieldAsText = true;
        options.ImagesFolder = extractionDir;

        doc.Save(extractionDir + "Document.SaveHtmlWithOptions.html", options);
    }

The test document contains the ‘bird’ and the ‘lake ice’ images.
The ‘lake ice’ is wrong using ShapeRenderer or Save as HTML,
But ‘bird’ works in Save as HTML.

Let me know if there’s any specific settings or alternative utility that I can use to extract the image with flatten effects on it.

Please see attached test document.test-document.zip (2.4 MB)

Thank you.

Regards,
Linx

@dcdeveloper,

We have just released a new 20.8 version of Aspose.Words for .NET API and after an initial test with the licensed latest (20.8) version of Aspose.Words for .NET, we were unable to reproduce this issue on our end. So, we suggest you please upgrade to the latest version.

@awais.hafeez,

Thank you for quick response.

Confirmed the image extracted are correct using 20.8 version of Aspose.Words, both in ShapeRenderer and save as HTML.

Definitely, we will upgrade to that version.

Regards,
Linx.

@dcdeveloper,

It is great that the latest (20.8) version of Aspose.Words for .NET resolves this problem on your end as well. Please let us know any time you may have any further queries in future.