Click here to Skip to main content
14,837,167 members
Articles / Programming Languages / C++
Posted 16 Dec 2005


17 bookmarked

Print a bitmap full page

Rate me:
Please Sign up or sign in to vote.
3.26/5 (8 votes)
16 Dec 2005
Print a bitmap full page.


Printing a bitmap isn't very difficult to do. I saw several solutions which used creation of DIB sections and quite some code. The trick here is to use the "LoadImage" API which can do that for you. This code uses portions of Chris Maunder's printing article:

void PrintBitmap(LPCTSTR filename) {
 CPrintDialog printDlg(FALSE);
 // Or get from user:
 // if (printDlg.DoModal() == IDCANCEL)   
 //        return; 
 CDC dc;
 if (!dc.Attach(printDlg.GetPrinterDC())) {
  AfxMessageBox(_T("No printer found!")); return;
 dc.m_bPrinting = TRUE; 
 DOCINFO di;    
 // Initialise print document details
 ::ZeroMemory (&di, sizeof (DOCINFO));
 di.cbSize = sizeof (DOCINFO);
 di.lpszDocName = filename; 
 BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job 
 // Get the printing extents
 // and store in the m_rectDraw field of a 
 // CPrintInfo object
 CPrintInfo Info;
 Info.SetMaxPage(1); // just one page 
 int maxw = dc.GetDeviceCaps(HORZRES);
 int maxh = dc.GetDeviceCaps(VERTRES); 
 Info.m_rectDraw.SetRect(0, 0, maxw, maxh); 
 for (UINT page = Info.GetMinPage(); page <= 
      Info.GetMaxPage() && bPrintingOK; page++) {
  dc.StartPage();    // begin new page
  Info.m_nCurPage = page;
  CBitmap bitmap;
  // LoadImage does the trick here, it creates a DIB section
  // You can also use a resource here
  // by using MAKEINTRESOURCE() ... etc. 
   ::GetModuleHandle(NULL), filename, IMAGE_BITMAP, 0, 0, 
    AfxMessageBox(_T("Error loading bitmap!")); return;
   BITMAP bm;
   int w = bm.bmWidth; 
   int h = bm.bmHeight; 
   // create memory device context
   CDC memDC; 
   CBitmap *pBmp = memDC.SelectObject(&bitmap);
   // now stretchblt to maximum width on page
   dc.StretchBlt(0, 0, maxw, maxh, &memDC, 0, 0, w, h, SRCCOPY); 
   // clean up
   bPrintingOK = (dc.EndPage() > 0);   // end page
 if (bPrintingOK)
   dc.EndDoc(); // end a print job
 else dc.AbortDoc();           // abort job. 

That's all there is to it. Have a nice day!


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


About the Author

Niek Albers
Web Developer
Netherlands Netherlands
Niek is the founder and programmer of and is working on many projects all the time. He makes a living by doing contractwork for others.

Comments and Discussions

QuestionHow Would This Be Adapted to Work with Other Image Types Pin
David Johns24-Sep-13 12:42
MemberDavid Johns24-Sep-13 12:42 
QuestionPrint a bitmap full page Pin
Member 93446368-Aug-12 10:04
MemberMember 93446368-Aug-12 10:04 
Generalprinted bitmaps have some colors modified Pin
Ștefan-Mihai MOGA21-Nov-06 0:23
professionalȘtefan-Mihai MOGA21-Nov-06 0: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.