Save doc as image. Resize?

Hello,

i am currently evaluating Aspose products.
Is there an easy way to set the generated images to a fixed size. Best would be a fixed width.
I looked into ImageSaveOptions but there seems to be nothing.

The same would be interesting for xls und pdf.

I need to store the documents as images for some special equipment with limited display options.

best regards

Armin

Hi
Thanks for your inquiry. I think, you can try using RenderToSize method:
https://reference.aspose.com/words/net/aspose.words/document/rendertosize/
Please let me know if you need more assistance, I will be glad to help you.
Best regards,

Hello,

thanks for your fast help. I have seen this already but hoped for a simpler way.
Espacially because the save function is very simple and i found this function in other (not so mighty tools) converting fileformats into images.

best regards

Armin

Hi Alorezn
Thnaks for your inquuiry.
Could you please clarify the options that you would need to make this method easier to use? We may log a request for the functionality or we maybe able to suggest a simple wrapper method of the above function to achieve what you need.
Thanks,

Hello,

thanks again for the fast answer.

You already have the ImageSaveOptions. If you enhance it with an size option everything would be fine for me.

It should be possible to set a fixed pixel value for both dimensions (x/y). Like “FixedWidth” and “FixedHeight”. If one of these two values is not set (0) the other dimension should be calculated to preserve the aspect ration. If none is set it works like the current version.

best regards

Armin

Hi

Thanks for your inquiry. I think, in your case you can easily achieve what you need using RenderToSize method. You can wrap it into your own method. For instance see the following code:

private void SaveToImage(Document doc, int pageIndex, string imageName, int width, int height)
{
    using (Bitmap bmp = new Bitmap(width, height))
    {
        // User has some sort of a Graphics object. In this case created from a bitmap.
        using (Graphics gr = Graphics.FromImage(bmp))
        {
            gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            gr.PageUnit = GraphicsUnit.Pixel;
            doc.RenderToSize(pageIndex, gr, 0f, 0f, width, height);
            bmp.Save(imageName);
        }
    }
}

Best regards,

Hello,

thanks again. But there seem to be a problem with the alpha channel in the generated image. I can see on the images only the columns (its a protocol doc with tables) with a different background color and the images. Everyting else is black. With png as fileformat i can see everything in some viewers and with gimp i can remove the alpha channel. After this it is ok in every viewer. As jpg it does not work in any viewer.

best regards

Armin

Hi there,
Thanks for your inquiry.
Please try using the modified code below:

private static void SaveToImage(Document doc, int pageIndex, string imagePath, int width, int height)
{
    using (Bitmap bmp = new Bitmap(width, height))
    {
        // User has some sort of a Graphics object. In this case created from a bitmap.
        using (Graphics gr = Graphics.FromImage(bmp))
        {
            gr.FillRectangle(new System.Drawing.SolidBrush(doc.PageColor), 0, 0, width, height);
            gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            gr.PageUnit = GraphicsUnit.Pixel;
            doc.RenderToSize(pageIndex, gr, 0f, 0f, width, height);
            bmp.Save(imagePath);
        }
    }
}

Thanks,

Hello,

thanks for you fast help. But unfortunately it does not solve the problem. Another other idea?

best regards

Armin

Dear Armin,

Could you please provide us with doc file you try to save as image?
Thank you

Hello,

this should be no problem. But i dont want to link it public in this thread because its internal. How can i send it “private”?

best regards

Armin

Armin,

Please check your forum private message inbox.

Hello,

thanks. But i was able to reproduce the problem with a simple doc. It looks fine as png only in the xp “windows picture and fax viewer” / firefox. It looks bad in irfanview and paint. The jpeg looks bad on every viewer a have.

best regards

Armin

Dear Armin,

I can not reproduce your issue. May I ask you to send me code fragment you use andAspose.Words version?

Thank you.

Hello,

VS2008, Aspose Words 9.5.0.0.

static void Main(string[] args)
{
    string TestFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Dok2.doc");
    Document doc = new Document(TestFilePath);
    for (Int32 y = 0; y < doc.PageCount; y++)
    {
        SaveToImage(doc, y, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Word" + y.ToString("000") + ".jpg"), 600, 0);
    }
}
static void SaveToImage(Document doc, int pageIndex, string imageName, int width, int height)
{
    Aspose.Words.Rendering.PageInfo PInfo = doc.GetPageInfo(pageIndex);
    height = System.Convert.ToInt32(PInfo.HeightInPoints / (PInfo.WidthInPoints / width));
    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height))
    {
        // User has some sort of a Graphics object. In this case created from a bitmap.
        using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp))
        {
            gr.FillRectangle(new System.Drawing.SolidBrush(doc.PageColor), 0, 0, width, height);
            gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            gr.PageUnit = System.Drawing.GraphicsUnit.Pixel;
            doc.RenderToSize(pageIndex, gr, 0f, 0f, width, height);
            bmp.Save(imageName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}

best regards

Armin

Dear Armin,

I have reproduced the issue. The problem here is that jpeg does not support transparency and makes it by default black.

So what you need is to set document background as white:

doc.PageColor = Color.White;

Please check that on your site.

Hello,

thanks again. It helped. But it seems to be a problem with imageformats supporting tranparency too.
Please try the png i posted before with Microsoft Paint. I tried it with 5.1. unter XP SP3 and the background is black. Same with Irfanview 4.25.

best regards

Armin

Hi Armin,

Thank you for additional information. Just to clarify a bit, this is not restriction or issue in Aspose.Words. This is some kind of specific of GDI. When you use alpha channel and save the image to JPEG, for example, transparent color will be black.
Best regards,

Hallo,

thanks for the answer. As you can see in my example code i did not use an alpha channel (as far is i know). If there is some alpha channel it must be added someweher else. I did not create the image. doc.RenderToSize created it. So there is the question of the source of the alpha channel
And more important. If there is some alpha channel it should work in every major viewer. And for me it does not work.

best regards

Armin

Hi

Thank you for additional information. Alpha channel (transparency) is specified in background of your document. That is why resetting background color to white resolves the problem.
Best regards,