Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cannot print out an image file

Posted on 1998-04-10
1
Medium Priority
?
296 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 400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
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 a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 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