Solved

Cannot print out an image file

Posted on 1998-04-10
1
286 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 55
Host to IP 7 82
EvenOdd challenge 10 142
scoreUp challenge 14 68
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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