Image issue in version 5.2.0.0

Hi Aspose.Word Team,
I am currently testing a Pdf generation process by using Aspose.Words API for manipulating the word document template and convert it to a Pdf file.
In the word document template, I am doing a bookmark manipulation, where two images are inserted into the word document template. The two images are BarCode (by using Aspose.Barcode) and GraphChart (by using Aspose.Chart).
However, I had a problem with the Graph chart picture when converting the word document template into pdf file. The problem was, the Graph Chart picture was blurred.
Then, I discussed this issue in the Aspose.chart forum, suspecting that there was a problem with the Graph Chart image generation. They told me to use the EMF format so the quality will show better, but the result was still the same (i.e. the picture was blurry). Yesterday, Aspose.Chart team provides me with an example source code that proves the Graph chart picture generation works fine.
I have tested the pdf generation and found out that the Aspose.Chart team were using an older Aspose.Word API (i.e. ver 4.4.3.0), and the Graph chart picture is in excellent quality.
Then, I retry to generate the pdf by using the latest version of Aspose.Word (ver 5.2.0.0), again, the Graph Chart became fuzzy.
I have attached the example codes.
Any clues?
Thank you very much for your helps and supports.
Regards,
Jarry

Hello!
Thank you for your inquiry.
The graph chart is a metafile with resolution set to 133 DPI. Since version 4.4.3.0 logic has changed. Aspose.Words in the current implementation always converts metafiles to PNG on PDF export. You can see the image file in the same directory with main XML if you export to Aspose.Pdf XML format. Resulting raster image quality is dependent on the output resolution.

  1. If resolution is not specified in the image itself then output resolution is taken from the following property:
    doc. SaveOptions.PdfExportMetafileResolution
  2. If resolution is specified as in your case then the minimum of these two values is taken.

We don’t see the reason to scale images with higher resolution than specified in the file and what is intended for them. This behavior is by design. You can remove resolution properties from the metafile before you insert it to the document. If both vertical and horizontal resolution are zeroes you can easily control quality by increasing PdfExportMetafileResolution property. Default is 300 DPI. This should be enough in most cases.

Another way is converting metafiles to raster externally, before you insert the images.
I expect one reasonable question. That’s great but you can ask why MS Word renders the metafile without blur effect. Most probably it completely ignores image resolution and uses some default. But we give the possibility to control resolution of individual images.

Please let me know if I can help you further.
Regards,

Hi Viktor,
Thank you very much for your reply and explanation.
So how do I make the quality of the GraphChart similar to the ver 4.4.3.0? because I don’t need to set any properties in the pdf generation in ver 4.4.3.0.
and because the GraphChart metafile resolution is specified, then (in ver 5.2.0.0), it will take the minimum of those two values (as you mentioned above).
Thus, is there any way to make the GraphChart image has the same picture quality as ver 4.4.3.0?
Thank you.
Regards,
Jarry

Hello again.
Unfortunately, using .NET classes you can only get horizontal and vertical resolution from metafile, not set these values. So the easiest way is rasterizing the metafile with resolution you’d like to have. See .NET classes Image and Bitmap for that purpose. It’s good to start with 300 DPI.

  1. Create source Image from file or stream containing the metafile.
  2. Create destination Bitmap with size and resolution you need. For Bitmap it is possible to set resolution (SetResolution).
  3. Copy Image to Bitmap with one of methods of Graphics class.
  4. Save the new Bitmap or add it directly to the document.
    Regards,

Hi Viktor,
No success, the image still blur. I think I didn’t do it properly.
I follow you instruction, and this is my Script:

chart.Save("…/…/chart.emf")
Dim chartBitmap_source As Bitmap = New Bitmap("…/…/chart.emf")
Dim chartBitmap As Bitmap = New Bitmap(300, 300)
chartBitmap.SetResolution(300, 300)
Dim v_part As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, 300, 300)
Dim g As Graphics = Graphics.FromImage(chartBitmap)
g.DrawImage(chartBitmap_source, v_part)
g.Dispose()
builder.InsertImage(chartBitmap)

Do you have an example script for the steps that you told me?
Thank You very much for your helps.
Regards,
Jarry

Hi again!
Basically you can start with this:

Image srcImage = Image.FromFile("chart.emf");
Bitmap dstImage = new Bitmap(newWidth, newHeight, newFormat);
dstImage.SetResolution(newResolution, newResolution);
using (Graphics gr = Graphics.FromImage(dstImage))
gr.DrawImage(srcImage, 0, 0, dstImage.Width, dstImage.Height);
dstImage.Save("chart.png", ImageFormat.Png);

Play with newWidth, newHeight, newFormat and newResolution variables. This is just an idea and similar code is utilized inside Aspose.Words.
It’s better to set newWidth and newHeight to values corresponding to destination size if you can determine it. In Aspose.Words destination image size in points is used for these parameters. I mean image in terms of MS Word and Aspose.Words, not GDI+. But you can experiment with any values. For newResolution it’s good to start with 300 DPI as I already advised. newFormat can be anything you’d like to have. But note that DrawImage won’t work with indexed formats. You can start with PixelFormat.Format32bppArgb and probably also experiment with others.
I think that would be good to ask in Aspose.Chart forum whether you can control destination image resolution or whether they can provide this feature.
Regards,

Hi Viktor,
It works perfectly! :slight_smile:
Thank you very much for your helps and supports, I’ll ask Aspose.Chart if they can provide the new functionality.
Best Regards,
Jarry

Hello Jarry!
After some circumstances I managed to change default behavior regarding image resolution. From now metafiles will be scaled to the resolution given in SaveOptions regardless of their “built-in” resolution. I think that’s exactly what you asked about. Once any other cases come up important we’ll consider additional parameterization.
The issue is logged as #5277 in our defect database and implemented in the current codebase. New implementation will be available with the next hotfix in about 2-3 weeks.
Best regards,

The issues you have found earlier (filed as 5277) have been fixed in this update.