Click here to Skip to main content
15,886,063 members
Articles / Desktop Programming / MFC
Article

Print Bitmaps without Doc/View Framework

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
6 Jul 2000 168.8K   2.4K   33   29
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


Written By
Technical Lead Telvent
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
News^_*MANY ERRPRS!!! [modified] Pin
songzd12-Oct-06 17:34
songzd12-Oct-06 17:34 
GeneralCancelling the entire print job Pin
conacher10-Feb-06 4:53
conacher10-Feb-06 4:53 
GeneralPrinting to file Pin
anup_zade6-Jul-05 4:49
anup_zade6-Jul-05 4:49 
QuestionPrinting in tile pages?? Pin
sudhirpatel20-Jan-05 19:00
sudhirpatel20-Jan-05 19:00 
GeneralTotal Page Bug. Pin
promanx2521-Jul-03 1:54
promanx2521-Jul-03 1:54 
QuestionPrinting a large BMP in one page --scaling??? Pin
fordge9-Jan-03 1:27
fordge9-Jan-03 1:27 
Generalhelp me Pin
Anonymous5-Nov-02 15:15
Anonymous5-Nov-02 15:15 
GeneralDoes not work on an HP 940c Pin
ScorpioMidget10-Oct-02 2:29
ScorpioMidget10-Oct-02 2:29 
GeneralRe: Does not work on an HP 940c Pin
ScorpioMidget10-Oct-02 10:26
ScorpioMidget10-Oct-02 10:26 
GeneralRe: Does not work on an HP 940c Pin
Shulik5-May-03 11:17
Shulik5-May-03 11:17 
I also had the same problem. The solution is to fill the Printer's DC with white. (Use GetdeviceCaps() to get width & height)
Its work for me in any project.
Hope its helped.
GeneralRe: My project does not work on an HP Laserjet1200 Pin
ikoai7424-Nov-03 1:39
ikoai7424-Nov-03 1:39 
GeneralBitMap Printing NOT OK if Static Link Pin
cts31-Jan-02 12:52
cts31-Jan-02 12:52 
GeneralPrinting in Monochrome Pin
GaryS30-Jan-02 14:23
GaryS30-Jan-02 14:23 
QuestionPrinting a bitmap from a file? Pin
Shilantra23-Feb-01 5:26
Shilantra23-Feb-01 5:26 
AnswerRe: Printing a bitmap from a file? Pin
15-Jun-01 19:27
suss15-Jun-01 19:27 
AnswerRe: Printing a bitmap from a file? Pin
hj31230-Aug-01 22:14
hj31230-Aug-01 22:14 
AnswerRe: Printing a bitmap from a file? Pin
27-Feb-02 22:45
suss27-Feb-02 22:45 
AnswerRe: Printing a bitmap from a file? Pin
12-Apr-02 1:48
suss12-Apr-02 1:48 
AnswerRe: Printing a bitmap from a file? Pin
Cristian Plesea9-Jun-03 4:51
Cristian Plesea9-Jun-03 4:51 
GeneralRe: Printing a bitmap from a file? Pin
Vinaya16-Mar-05 22:24
Vinaya16-Mar-05 22:24 
GeneralBUG & FIX: Wont' work on Network Pin
13-Jan-01 14:48
suss13-Jan-01 14:48 
GeneralRe: BUG & FIX: Wont' work on Network Pin
stephane padovani1-Oct-02 2:55
sussstephane padovani1-Oct-02 2:55 
GeneralRe: BUG & FIX: Wont' work on Network Pin
ScorpioMidget10-Oct-02 2:54
ScorpioMidget10-Oct-02 2:54 
GeneralRe: BUG & FIX: Wont' work on Network Pin
ScorpioMidget10-Oct-02 10:26
ScorpioMidget10-Oct-02 10:26 
GeneralJust what I was looking for Pin
10-Dec-00 22:35
suss10-Dec-00 22:35 

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.