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.
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.
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,
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.
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);
}
}
}
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.
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. 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.
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);
}
}
}
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.
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,
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.
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,
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.