Click here to Skip to main content
15,884,177 members
Articles / Desktop Programming / MFC
Article

Printing using GDI+ : a few tips

Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
10 Sep 20022 min read 164.8K   1.4K   44   23
A few "specialized" tips on printing figures using GDI+

Introduction

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 section.

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:

CDC dc;
Graphics graphics;

Setting the mapping modes

The mapping modes of dc and graphics must be tuned together:

dc.SetMapMode(MM_TEXT);
graphics.SetPageUnit(UnitDocument);

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:

  1. Get the dpi ratio dpiRatio:
    CPrintDialog MyPrintDialog;
     ...
     // the dpi of printer is enclosed in DEVMODE structure,
     DEVMODE* pDev=MyPrintDialog.GetDevMode();
    // getting dpi ratio between 300dpi and current printer dpi
    double dpiRatio=300.0/pDev->dmPrintQuality;
    // deallocating memory for pDev
    VERIFY(GlobalUnlock(pDev));
    
  2. Setting page scale in to graphics
    graphics.SetPageScale(dpiRatio);
    

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)
{
	// This will calculate the font size for the printer that is specified
	// by a point size.
	//
	// if points is:
	//  (-) negative uses height as value for Net Font Height (ie. point size)
	//	(+) positive height is Total Height plus Leading Height!
	ASSERT(hDC);
	
	POINT size;
	int logPixelsY=::GetDeviceCaps(hDC, LOGPIXELSY);
	size.x = size.y = MulDiv(points, logPixelsY, 72);

	// here we scale the font...
	return (float)floor(size.y*dpiRatio);
}

Create font for printing

When creating a font, use the following unit:
Unit fontUnit = m_pGraphics->GetPageUnit();
// if fontUnit is UnitDisplay, then specify UnitPixel, 
// otherwise you'll get a "InvalidParameter" from GDI+
if (fontUnit == UnitDisplay)
fontUnit = UnitPixel;
// classical constructor use, lfHeight is the font height
Font font(&fontFamily, CreateFontSize(hDC, 
          lfHeight, dpiRatio), FontStyleRegular, fontUnit);

Credits

A lot of useful article available on the Printing section:

Update History

11/09/2002Fixed problem of text changing size when playing with the zoom in preview mode. Updated the demo project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey18-Feb-12 0:09
professionalManoj Kumar Choubey18-Feb-12 0:09 
GeneralAnother solution: Pin
ommail18-Sep-08 21:40
ommail18-Sep-08 21:40 
GeneralRe: Another solution: Pin
ShaunBloodsworth10-May-12 10:54
ShaunBloodsworth10-May-12 10:54 
GeneralError Pin
danandu17-Apr-08 0:14
danandu17-Apr-08 0:14 
I am getting an error saying "gdiplus.h" no such file or directory Frown | :(
GeneralGDI/GDI+ printing stuff... Pin
Alberto_Canabal23-Jan-06 2:20
Alberto_Canabal23-Jan-06 2:20 
GeneralPrinting to file Pin
anup_zade4-Jul-05 23:27
anup_zade4-Jul-05 23:27 
Generaltransparency Pin
Dieter Hammer7-Dec-04 3:31
Dieter Hammer7-Dec-04 3:31 
Generalprinting problems using GDI+ enhanced metafile Pin
P e t e r20-Oct-04 1:55
P e t e r20-Oct-04 1:55 
QuestionHow to save GDI+ bmp as CMYK color GDI+ bmp Pin
pubududilena27-May-04 23:49
pubududilena27-May-04 23:49 
GeneralAnother Solution Pin
FlyingHero5-Nov-03 16:10
FlyingHero5-Nov-03 16:10 
GeneralRe: Another Solution Pin
bob1697215-Jun-05 12:18
bob1697215-Jun-05 12:18 
GeneralRe: Another Solution Pin
Lars [Large] Werner19-Jun-06 9:39
professionalLars [Large] Werner19-Jun-06 9:39 
GeneralRe: Another Solution Pin
Neal175912-Jul-06 9:39
Neal175912-Jul-06 9:39 
GeneralPreview problem Pin
DrFretBoard23-Dec-02 1:55
DrFretBoard23-Dec-02 1:55 
Generalquestion about CPrintDialog and DEVMODE Structure Pin
lavocat2-Dec-02 0:38
lavocat2-Dec-02 0:38 
GeneralNew to GDI+ Printer Pin
larsi21-Oct-02 2:59
larsi21-Oct-02 2:59 
GeneralRe: New to GDI+ Printer Pin
wickywang28-Dec-04 21:57
wickywang28-Dec-04 21:57 
GeneralText Printing As Garbage Pin
Brian Vollmer1-Oct-02 11:42
Brian Vollmer1-Oct-02 11:42 
GeneralComments Pin
Nish Nishant1-Aug-02 14:42
sitebuilderNish Nishant1-Aug-02 14:42 
GeneralPrint preview... Pin
Jonathan de Halleux10-Sep-02 23:29
Jonathan de Halleux10-Sep-02 23:29 
GeneralSome good tips Pin
Roger Allen30-Jul-02 2:30
Roger Allen30-Jul-02 2:30 
Generalnice but... Pin
Mario M.29-Jul-02 14:38
Mario M.29-Jul-02 14:38 
GeneralDone Pin
Jonathan de Halleux30-Jul-02 4:23
Jonathan de Halleux30-Jul-02 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.