Solved

Cannot print out an image file

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

816 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

7 Experts available now in Live!

Get 1:1 Help Now