Image Extraction (Multiple GroupShapes)

Respected Sir/Madam,

I would like to produce a high quality image from the document “Shapes-modified.docx”

I am using the following API but the resulting image is of not good quality and is too large.

doc.Save(Path.Combine(cwd, "output.png"));

I would like to obtain a true transparent PNG/SVG image from the document. There are 4 group shapes in this document. I am unable to use ShapeRenderer to obtain image because there are two overlapping shapes in this group. When I try to add these two shapes as children to the GroupShape it is not working either.

Highly appreciate your help in this regard.

Thanks & Regards,
Sandeep

Hi Sandeep,

Thanks for your inquiry. Please set the ImageSaveOptions.Resolution to 300 as shown below to get the good quality image. If you want to remove the empty space from the image, please use the following highlighted code snippet. Hope this helps you.

Document doc = new Document(MyDir + @"Shapes-modified.docx");
PageInfo pageInfo = doc.GetPageInfo(0);
const float MyScale = 1.0f;
// Let's say we want the image at this resolution.
const float MyResolution = 300.0f;
Size pageSize = pageInfo.GetSizeInPixels(MyScale, MyResolution);
MemoryStream stream = new MemoryStream();
using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height))
{
    img.SetResolution(MyResolution, MyResolution);
    using (Graphics gr = Graphics.FromImage(img))
    {
        // You can apply various settings to the Graphics object.
        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        // Fill the page background.
        gr.FillRectangle(Brushes.Transparent, 0, 0, pageSize.Width, pageSize.Height);
        // Render the page using the zoom.
        doc.RenderToScale(0, gr, 0, 0, MyScale);
    }
    img.Save(stream, ImageFormat.Png);
}
Bitmap croppedImage;
// Load the image into a new bitmap.
using (Bitmap renderedImage = new Bitmap(stream))
{
    // Extract the actual content of the image by cropping transparent space around
    // the rendered shape.
    Rectangle cropRectangle = FindBoundingBoxAroundNode(renderedImage);
    croppedImage = new Bitmap(cropRectangle.Width, cropRectangle.Height);
    croppedImage.SetResolution(300, 300);
    // Create the final image with the proper background color.
    using (Graphics g = Graphics.FromImage(croppedImage))
    {
        g.Clear(Color.White);
        g.DrawImage(renderedImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height), cropRectangle.X, cropRectangle.Y, cropRectangle.Width, cropRectangle.Height, GraphicsUnit.Pixel);
    }
}
croppedImage.Save(MyDir + "out.png");
public static Rectangle FindBoundingBoxAroundNode(Bitmap originalBitmap)
{
    Point min = new Point(int.MaxValue, int.MaxValue);
    Point max = new Point(int.MinValue, int.MinValue);
    for (int x = 0; x < originalBitmap.Width; ++x)
    {
        for (int y = 0; y < originalBitmap.Height; ++y)
        {
            // Note that you can speed up this part of the algorithm by using LockBits and unsafe code instead of GetPixel.
            Color pixelColor = originalBitmap.GetPixel(x, y);
            // For each pixel that is not transparent calculate the bounding box around it.
            if (pixelColor.ToArgb() != Color.Empty.ToArgb())
            {
                min.X = Math.Min(x, min.X);
                min.Y = Math.Min(y, min.Y);
                max.X = Math.Max(x, max.X);
                max.Y = Math.Max(y, max.Y);
            }
        }
    }
    // Add one pixel to the width and height to avoid clipping.
    return new Rectangle(min.X, min.Y, (max.X - min.X) + 1, (max.Y - min.Y) + 1);
}