Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when rendering metafile, the texts are too large

Tags:

delphi

I'm having a problem with metafile rendering in my Delphi XE application.

the problem is that when I'm rendering the metafile, the texts are too large. Irfanview and FastReports render it like this:

look at the text to the left of the graph

windows 7 Paint renders it fine: (here's what the text should look like)

windows 7 Paint renders it fine

Any ideas what is causing this?

Thank you!

like image 625
X-Ray Avatar asked Jan 09 '12 19:01

X-Ray


2 Answers

emf files are just a list of GDI commands. In fact, they can be "played back" very easily by the system, using standard Windows GDI command (in Delphi, a TMetaFile is just a wrapper around those APIs).

When IrfanView or FastReport renders the metafile content, they just use Windows GDI corresponding commands. When Windows 7 Paint renders the metafile content, it uses the GDI+ renderer. I even think it internally convert the emf file into emf+ format, then renders it with anti-aliaising using GDI+.

So if the emf file renders incorrectly in IrfanView or FastReport, I suspect this is because your metafile is not well formed: the third party graphic engine you are using is producing non standard emf. A possible issue is that the font used is missing in the target system, and GDI does not substitute the font with the same as GDI+ does.

Another possibility is that the emf file is maybe a dual format: it contains both emf format (which was not properly created so is not rendered correctly using GDI) and emf+ format (which is rendered as expected using GDI+). Normally this dual emf/emf+format should not exist: even the official GDI+ library does not allow to save its metafile content in emf+. This is some kind of "monster" format, created by your third-party library.

I suggest the following:

  • Download and check your emf file with EmfExplorer;
  • Try to use GDI+ to render the metafile;
  • Try to use the emf to emf+ converter API.

For using GDI+, take a look at an Open Source SynGdiPlus unit: it will add GDI+ anti-aliaising to your produced bitmap. It is able to convert emf to emf+. It will use native Vista/Seven API (just like Windows 7 paint), or plain Delphi code under Windows XP.

like image 135
Arnaud Bouchez Avatar answered Oct 10 '22 23:10

Arnaud Bouchez


The cause probably is that different monitor size and screen resolution ratio. GDI has parameters HORZRES, HORZSIZE, VERTRES, VERTSIZE. In most cases HORZRES/VERTRES, HORZSIZE/VERTSIZE (resolution ratio and screen ratio) are the same and everything works well... However if they are different (I have found some examples of this happening on servers) then the pixel is assumed to be rectangular, this causes LOGFONT.lfWidth to be calculated "wrongly". LOGFONT.lfWidth determines the aspect ratio of characters and this finally causes the weird looking letters.

One solution is to change the resolution so that HORZRES/VERTRES, HORZSIZE/VERTSIZE match. The other solution is to use a printer DC to render your things. Setting the LOGFONT.lfWidth value explicitly may help. Also updating video driver may help.

I encountered the same problem, I had temporary drawing to a metafile canvas that used GetDC(0) as reference instead of printer DC. Here are some links that have the same problem:

  • http://us.generation-nt.com/answer/createenhmetafile-app-running-via-remote-desktop-help-27451862.html
  • http://social.msdn.microsoft.com/Forums/en/windowsgeneraldevelopmentissues/thread/a4a12ed8-c673-4a5c-94e1-7165b16f6955
like image 38
Egon Avatar answered Oct 10 '22 22:10

Egon