Click here to Skip to main content
Licence 
First Posted 5 Jul 2000
Views 129,168
Bookmarked 31 times

Print Bitmaps without Doc/View Framework

By | 6 Jul 2000 | Article
How to print a user-drawn bitmap without relying on the Doc/View's functions.

Introduction

In this article, I demonstrate how to print a user-drawn bitmap without relying on the Doc/View's functions. After reading this, you’d see every step in manually getting a printer to work. Basically, we need to deal with a few things:

  1. Get a printer device context and calculate the total number of sheets to be printed before opening the printer dialog.
  2. Calculate the portion (dimension) of the bitmap to be sent to the printer to fit into one print sheet (paper page). Avoid printing distortion.

All the code segments are contained in a function (no parameter) under View.

CDC memDC;
CClientDC dc(this);
//...
memDC.CreateCompatibleDC( &dc );
CBitmap * bitmap = new CBitmap();
bitmap->CreateCompatibleBitmap(&dc,bmpWidth, bmpHeight);
CBitmap * pOldBitmap = (CBitmap *) memDC.SelectObject( bitmap );
if (pOldBitmap == NULL) // if bitmap is very big, better check this !
{
    memDC.DeleteDC();
    delete bitmap;
    AfxMessageBox("Not enough resource for the bitmap.....");
    return;
}
//draw bitmap here, or it can be done in 
//another function outside the View

The above code draws an ellipse. For more sophisticated drawings, it can be done elsewhere, as long as we have the pointer to the bitmap. One "unusual" thing is the check on pOldBitmap. One doesn’t see this very often. However, as the warning message says, it could happen when the Windows bottle is unable to hold the big picture.

Now we drive the printer to work. First get a printer DC from the default printer, and with this DC, collect some printer information. This is for calculating the actual pages to be printed and other parameters.

CDC prtDC;
if( AfxGetApp()->GetPrinterDeviceDefaults(&printInfo.m_pPD->m_pd) )
{
    HDC hDC = printInfo.m_pPD->m_pd.hDC;
    if (hDC == NULL)
        hDC = printInfo.m_pPD->CreatePrinterDC();
    if(hDC !=NULL)
    {
        prtDC.Attach(hDC);
        //Get paper size and resolution with prtDC;
        //...
    }
    else 
    {
        AfxMessageBox("Can not find printer. 
            Please check installed/default printers.");
        return;
    }
}

Now we calculate the total pages to be printed (refer to source code file for the parameters). This way, we can activate the page info radio buttons in printer dialog:

int total_pages = (bmpWidth * ratio_x + paper_width - 1 ) / paper_width;

Finally, we stuff the printer dialog, and pop it up:

CPrintDialog prtDlg(FALSE, PD_PAGENUMS);
prtDlg.m_pd.nMinPage = 1;
prtDlg.m_pd.nMaxPage = total_pages;
prtDlg.m_pd.nFromPage = 1;
prtDlg.m_pd.nToPage = total_pages;

if(prtDlg.DoModal() == IDOK )
{
    //...
}
else
    return;  //Cancel button pressed, don't forget this!

The following code segment is responsible for starting the print job, sending data to the printer, and ending the job when all is printed. Actual printer operation (the noise) is not started until StartPage method is called, which sends data for printing one sheet (paper) only. To print more pages, repeat calling this method. In using StretchBlt method, care is taken for the last page. Unlike other bitmap portions with default width corresponding to the paper width, we need to find its width and StretchBlt it to appropriate dimensions on the paper, instead of filling the whole paper width, the latter may cause distortion.

if(prtDC.StartDoc(&di) == -1)
{
    AfxMessageBox("Printing error occurred. Unable to find printer.");
    prtDC.Detach();
    prtDC.DeleteDC();
    return;
}

prtDC.SetMapMode(MM_TEXT);  // 1 : 1 mapping

int i = 0;
for(i = 0; i < total_pages; i++)
{
    prtDC.StartPage();  
    strPageNumber.Format("%d of %d", ++printed_pages, total_print_pages );
    if( i == (total_pages - 1) && total_pages > 1 ) //last page
    {
        int last_bmpWidth = bmpWidth - paper_width / ratio_x * i;
        prtDC.StretchBlt(0, 0, last_bmpWidth * ratio_x, bmpHeight* ratio_y,
            &memDC, paper_width * i / ratio_x, 0, 
            last_bmpWidth, bmpHeight, SRCCOPY);
    }
    else
        prtDC.StretchBlt(0, 0, paper_width, bmpHeight* ratio_y, &memDC, 
            paper_width * i / ratio_x, 0, 
            paper_width / ratio_x , bmpHeight, SRCCOPY);
    prtDC.TextOut(page_info_rect.left, page_info_rect.top, strPageNumber );

    prtDC.EndPage();
}

//clean up.

That’s it. One thing I omitted (for clarity) is printing the selected pages (returned by the dialog). Also, one can add a progress dialog which shows the printing pages and gives the user a Cancel button to stop the loop. This had been well covered by other authors. See, for example, Paul DiLasci’s article in MSJ(7/98).

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

About the Author

Weimin Chen

Technical Lead
Telvent
Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
News^_*MANY ERRPRS!!! [modified] Pinmembersongzd17:34 12 Oct '06  
GeneralCancelling the entire print job Pinmemberconacher4:53 10 Feb '06  
GeneralPrinting to file Pinmemberanup_zade4:49 6 Jul '05  
QuestionPrinting in tile pages?? Pinmembersudhirpatel19:00 20 Jan '05  
GeneralTotal Page Bug. Pinmemberpromanx251:54 21 Jul '03  
QuestionPrinting a large BMP in one page --scaling??? Pinmemberfordge1:27 9 Jan '03  
Generalhelp me PinsussAnonymous15:15 5 Nov '02  
GeneralDoes not work on an HP 940c PinmemberScorpioMidget2:29 10 Oct '02  
GeneralRe: Does not work on an HP 940c PinmemberScorpioMidget10:26 10 Oct '02  
GeneralRe: Does not work on an HP 940c PinsussSmhulik11:17 5 May '03  
GeneralRe: My project does not work on an HP Laserjet1200 Pinmemberikoai741:39 24 Nov '03  
GeneralBitMap Printing NOT OK if Static Link Pinmembercts12:52 31 Jan '02  
GeneralPrinting in Monochrome PinmemberGaryS14:23 30 Jan '02  
QuestionPrinting a bitmap from a file? PinmemberShilantra5:26 23 Feb '01  
AnswerRe: Printing a bitmap from a file? Pinmemberramakrishna.b19:27 15 Jun '01  
AnswerRe: Printing a bitmap from a file? Pinmemberhj31222:14 30 Aug '01  
AnswerRe: Printing a bitmap from a file? PinmemberNeeraj Joshi22:45 27 Feb '02  
AnswerRe: Printing a bitmap from a file? PinmemberHeiko1:48 12 Apr '02  
AnswerRe: Printing a bitmap from a file? PinmemberCristian Plesea4:51 9 Jun '03  
GeneralRe: Printing a bitmap from a file? PinmemberVinaya22:24 16 Mar '05  
GeneralBUG & FIX: Wont' work on Network PinmemberScott Lloyd14:48 13 Jan '01  
GeneralRe: BUG & FIX: Wont' work on Network Pinsussstephane padovani2:55 1 Oct '02  
GeneralRe: BUG & FIX: Wont' work on Network PinmemberScorpioMidget2:54 10 Oct '02  
GeneralRe: BUG & FIX: Wont' work on Network PinmemberScorpioMidget10:26 10 Oct '02  
GeneralJust what I was looking for Pinmemberkristof22:35 10 Dec '00  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 7 Jul 2000
Article Copyright 2000 by Weimin Chen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid