This articles gives some hints on printing figure made with GDI+. As anybody knows, printing is one of the most mysterious
feature in MFC (this is my point of view).
It is not well (at all) documented and in fact, the best examples can be found in CodeProject Printing
Since GDI+ is a new technology, it brings new problems when trying to print.
In the following, I will try to give some hints encountered when trying to print GDI+ stuff.
I will suppose that you have some basic knowledge about printing, especially that you have read
one of the following articles, so I won't have to discuss about getting a printer DC working and other details already
in those article but rather focus on GDI+ problem related:
In the following,
dc denotes the printer dc and
graphics denotes the GDI+ graphic context:
Setting the mapping modes
The mapping modes of
graphics must be tuned together:
With those setting, each logical unit is converted to 1 device unit (
MM_TEXT) and one
device unit is 1/300 inch (UnitDocument). So we get 300dpi printing, great.
What about other DPIs?
Gulp, we got it working for 300dpi, but what about 600 dpi? or 1200 ?
After a few try and error, I figured out we had to do the dirty job ourselves, that is check for the dpi of printer and
scale accordingly the graphic:
- Get the dpi ratio
- Setting page scale in to
That ugly hack should do the work. Of course, any nicer method is welcome :-)
What about text?
Getting font size
Unfortunately, scaling the graphic is not sufficient and Dpi scaling has to be taken in account when computing the font size!
Here's a modification of the
CreateFontSize of Barnhart article.
As you see, the user has to pass the
dpiRatio to scale
the font accordingly:
int CreateFontSize(HDC hDC, int points, double dpiRatio)
int logPixelsY=::GetDeviceCaps(hDC, LOGPIXELSY);
size.x = size.y = MulDiv(points, logPixelsY, 72);
Create font for printing
When creating a font, use the following unit:
Unit fontUnit = m_pGraphics->GetPageUnit();
if (fontUnit == UnitDisplay)
fontUnit = UnitPixel;
Font font(&fontFamily, CreateFontSize(hDC,
lfHeight, dpiRatio), FontStyleRegular, fontUnit);
A lot of useful article available on the Printing section:
|11/09/2002||Fixed problem of text changing size when playing with the zoom in preview mode.
Updated the demo project.|