Solved

Cannot print out an image file

Posted on 1998-04-10
1
278 Views
Last Modified: 2013-11-20
Hello,

I am trying to do printing without the CView/CDocument architecture in my MDI application.  Below is my printing code.  Testing the printing code, the text "Hello World" prints out but the sample bitmap in the resource does not get printed.  What am I doing incorrectly?  The StretchBlt() fails, always returning a value of zero.


void CMainFrame::OnFilePrint()
{
      // TODO: Add your command handler code here
      CLog Log;
      teLogLevel eLogLevel = Log.GetLogLevel();
      char   pbuf[100] = "Hello World.";
      HDC    hdcPrn ;

      // Instantiate a CPrintDialog.
      CPrintDialog *printDlg =
            new CPrintDialog(FALSE, PD_ALLPAGES | PD_RETURNDC, NULL);

      // Initialize some of the fields in PRINTDLG structure.
      printDlg->m_pd.nMinPage = printDlg->m_pd.nMaxPage = 1;
      printDlg->m_pd.nFromPage = printDlg->m_pd.nToPage = 1;

      // Display Windows print dialog box.
      printDlg->DoModal();

      // Obtain a handle to the device context.
      hdcPrn = printDlg->GetPrinterDC();
      if (hdcPrn != NULL)
      {
            if (eLogLevel >= eMinimum)
                  Log.WriteLog("Start printing process.");
            CDC *pDC = new CDC;
            pDC->Attach (hdcPrn);// attach a printer DC

            DOCINFO docInfo;
            memset(&docInfo, 0, sizeof(DOCINFO));
            docInfo.cbSize = sizeof(DOCINFO);
            char szName[] = _T("Test");
            docInfo.lpszDocName = szName;
            docInfo.lpszOutput = NULL;
            pDC->StartDoc(&docInfo);// begin a new print job
                                                                              // for Win32 use
                                                                              // CDC::StartDoc(LPDOCINFO) override

            SetPrintAlign(pDC, hdcPrn);// Set the printing alignment

            //need to figure out how many pages to print based on the printing
            //layout selected by the user
            //for testing now, print each image on one page and the study information
            //on each page as well

            int iRetVal = pDC->StartPage();          // begin a new page
            if (iRetVal < 0)
            {//error occured in preparing the printer driver to receive data
                  pDC -> EndDoc();
                  pDC->Detach();             // detach the printer DC
                  delete pDC;
                  if (eLogLevel >= eMinimum)
                        Log.WriteLog("Error occured in preparing the printer driver to receive data.");
                  delete printDlg;
                  return;
            }
      HBITMAP hPixmap = LoadBitmap (AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_COLORUP));
  if(hPixmap == NULL) return;
  HDC tempDC = CreateCompatibleDC(pDC -> GetSafeHdc());
  HBITMAP oldBM = (HBITMAP)::SelectObject(tempDC, hPixmap);
  BITMAP bm;
  GetObject (hPixmap, sizeof(BITMAP), (LPSTR)&bm);

  int ret = 0;
      pDC -> SetStretchBltMode(COLORONCOLOR);
  ret = StretchBlt(hdcPrn, 10, 10,
                              bm.bmWidth, bm.bmHeight,
                      tempDC, 0, 0,
                      bm.bmWidth, bm.bmHeight, SRCCOPY);
  SelectObject(tempDC, oldBM);

  DeleteDC(tempDC);
            
            pDC->TextOut(10, 10, pbuf);// write the string in pbuf
            iRetVal = pDC->EndPage();            // end a page
            if (iRetVal < 0)
            {//error occured in writing to a page
                  pDC -> EndDoc();
                  pDC->Detach();             // detach the printer DC
                  delete pDC;
                  if (eLogLevel >= eMinimum)
                        Log.WriteLog("Error occured in writing to a page.");
                  delete printDlg;
                  return;
            }

            pDC->EndDoc();             // end a print job

            pDC->Detach();             // detach the printer DC
            delete pDC;
      }

      delete printDlg;
}//endof OnFilePrint


void CMainFrame::SetPrintAlign(CDC *pDC, HDC hdcPrn)
{
      short cxPage, cyPage;

      cxPage = ::GetDeviceCaps (hdcPrn, HORZRES) ;
      cyPage = ::GetDeviceCaps (hdcPrn, VERTRES) ;
/*
      pDC->SetMapMode(MM_ISOTROPIC) ;
      pDC->SetWindowExt (1000, 1000) ;
      pDC->SetViewportExt(cxPage / 2, -cyPage / 2) ;
      pDC->SetViewportOrg(cxPage / 2,  cyPage / 2) ;
*/
      pDC->SetMapMode(MM_ANISOTROPIC) ;
      pDC->SetWindowExt (2000, 2000) ;
      pDC->SetViewportExt(cxPage, -cyPage) ;
      pDC->SetViewportOrg(0,  cyPage) ;
      pDC->SetTextAlign(TA_BASELINE | TA_CENTER) ;
}//endof SetPrintAlign

Any help will be greatly appreciated and neck-saving!  Thank you.

Jennifer
0
Comment
Question by:jli
1 Comment
 

Accepted Solution

by:
yangjun earned 100 total points
ID: 1317623
I came across the same problem. The reason in my situation is in CreateCompatibleDC
If you print to Postscript printer, you can't print, since a PS printer is not a Raster printer! Use DIB instead of DIB can solve your problem!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 121
Perl Awk Need Help 3 109
Tab names are off by one 5 53
Prevent this page from creating additional dialogs. 3 403
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now