關於打印圖片的一個例子

DOCINFO docInfo;
 ZeroMemory(&docInfo, sizeof(docInfo));
 docInfo.cbSize = sizeof(docInfo);
 docInfo.lpszDocName = "GdiplusPrint";
 
 // Create a PRINTDLG structure, and initialize the appropriate fields.
 PRINTDLG printDlg;
 ZeroMemory(&printDlg, sizeof(printDlg));
 printDlg.lStructSize = sizeof(printDlg);
 printDlg.Flags = PD_RETURNDC;
 
 
 // Display a print dialog box.
 if(!PrintDlg(&printDlg))
 {
  printf("Failure/n");
 }
 else
 {
  // Now that PrintDlg has returned, a device context handle
  // for the chosen printer is in printDlg->hDC.
  //GetDefaultPrinter()
  CRect printClientRect;
  Rect printRect;
  int prHeight,prWidth,phyHeight,phyWidth;
  int hSize,wSize;
  
  CDC * printpCDC = CDC::FromHandle(printDlg.hDC);
  prWidth = printpCDC->GetDeviceCaps(HORZRES);
  prHeight = printpCDC->GetDeviceCaps(VERTRES);
  //phyHeight = printpCDC->GetDeviceCaps(PHYSICALHEIGHT);
  //phyWidth = printpCDC->GetDeviceCaps(PHYSICALWIDTH);
  
  wSize = printpCDC->GetDeviceCaps(HORZSIZE);
  hSize = printpCDC->GetDeviceCaps(VERTSIZE);
  
  int printx=printpCDC->GetDeviceCaps(LOGPIXELSX);
  int printy=printpCDC->GetDeviceCaps(LOGPIXELSY);
  int screenx=GetDC()->GetDeviceCaps(LOGPIXELSX);
  int screeny=GetDC()->GetDeviceCaps(LOGPIXELSY);
  
  float xRate = (float)((float)printx / (float)screenx);
  float yRate = (float)((float)printy / (float)screeny);
  
  //tmpwnd->GetClientRect(printClientRect);
  
  //printClientRect.left = (phyWidth - prWidth) / 2;
  //printClientRect.top = (phyHeight - prHeight) / 2;
  //printClientRect.right = printClientRect.left + prWidth;
  //printClientRect.bottom = printClientRect.top + prHeight;
  
  printClientRect.left = 0;
  printClientRect.top = 0;
  printClientRect.right = (float)(((float)wSize / (float)25.4)) * screenx;
  printClientRect.bottom = (float)(((float)hSize / (float)25.4)) * screeny;
  
  StartDoc(printDlg.hDC, &docInfo);
  StartPage(printDlg.hDC);
  Graphics graphics(printDlg.hDC);
  
  
 
  //prImage.GetVerticalResolution()
  //float wRateImage =  (float)prImage.GetHorizontalResolution();
  //float hRateImage =  (float)prImage.GetVerticalResolution();
  
 // GetImageRect(printClientRect,prImage.GetHeight(),prImage.GetWidth(),1,printRect);
  
  graphics.DrawImage(m_pbmpSrc, 0, 0);   //這裏使用GDI+來打印到打印機的內容

  //graphics->DrawImage();
  EndPage(printDlg.hDC);
  EndDoc(printDlg.hDC);
 }
 if(printDlg.hDevMode)
  GlobalFree(printDlg.hDevMode);
 if(printDlg.hDevNames)
  GlobalFree(printDlg.hDevNames);
 if(printDlg.hDC)
  DeleteDC(printDlg.hDC); 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章