Solved

Cannot print out an image file

Posted on 1998-04-10
1
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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