Print a Image[bmp, jpg, gif, png...] with full page by gdiplus

Print a Image[bmp, jpg, gif, png...] with full page by gdiplus

 

作者:陈明 [http://blog.csdn.net/qmroom]

(转载请注明出处)

 

完整代码下载:http://download.csdn.net/source/948960

 

源代码中多了两行

#include "Common.h"
#include "Setting.h"

删掉就行了

 

在网上搜索了很多,没有发现直接打印 jpg 的例子,在研究之后发现可以用GDI来绘制打印,完整代码如下:

  1. #include <atlbase.h>
  2. #include <Windows.h>
  3. #include <winspool.h>
  4. #include <gdiplus.h>
  5. #include <Gdiplusgraphics.h>
  6. #pragma comment(lib, "Winspool.lib")
  7. #pragma comment(lib, "gdiplus.lib")
  8. //打印一张图片:  
  9.  BOOL PrintImage(LPCTSTR filename) 
  10.  {
  11.   if (!FileExist(filename))
  12.   {
  13.    SetLastError(2); //系统找不到指定的文件
  14.    return FALSE;
  15.   }
  16.   CPrintDialog printDlg(FALSE);
  17.   printDlg.GetDefaults();
  18.   // Or get from user:
  19.   //INT_PTR result = printDlg.DoModal();
  20.   //if (result == IDCANCEL)
  21.   // return;
  22.   CDC dc;
  23.   if (!dc.Attach(printDlg.GetPrinterDC())) 
  24.   {
  25.    SetLastError(2150); //打印机不存在
  26.    ::MessageBox(NULL, TEXT("打印机不存在"), TEXT("Error"), MB_OK|MB_ICONINFORMATION);
  27.    return FALSE;
  28.   } 
  29.   dc.m_bPrinting = TRUE; 
  30.   DOCINFO di;    
  31.   // Initialise print document details
  32.   ::ZeroMemory (&di, sizeof (DOCINFO));
  33.   di.cbSize = sizeof (DOCINFO);
  34.   di.lpszDocName = filename; 
  35.   BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job 
  36.   // Get the printing extents
  37.   // and store in the m_rectDraw field of a 
  38.   // CPrintInfo object
  39.   CPrintInfo Info;
  40.   Info.SetMaxPage(1); // just one page 
  41.   float maxw = static_cast<float>(dc.GetDeviceCaps(HORZSIZE) / 0.254);
  42.   float maxh = static_cast<float>(dc.GetDeviceCaps(VERTSIZE) / 0.254);
  43.   Info.m_rectDraw.SetRect(0, 0, static_cast<int>(maxw), static_cast<int>(maxh));
  44.   RectF rectf(0, 0, maxw, maxh);
  45.   for (UINT page = Info.GetMinPage(); page <= 
  46.    Info.GetMaxPage() && bPrintingOK; page++) 
  47.   {
  48.    dc.StartPage();    // begin new page
  49.    Info.m_nCurPage = page;
  50.    {
  51.     Graphics graphics(dc.GetSafeHdc());
  52.     Image img(filename);
  53.     graphics.DrawImage(&img, rectf, 0, 0, (REAL)img.GetWidth(), (REAL)img.GetHeight(), UnitPixel);
  54.    }
  55.    bPrintingOK = (dc.EndPage() > 0);   // end page
  56.   } 
  57.   if (bPrintingOK)
  58.   {
  59.    dc.EndDoc(); // end a print job
  60.    return TRUE;
  61.   }
  62.   else 
  63.   {
  64.    dc.AbortDoc();           // abort job. 
  65.    return FALSE;
  66.   }
  67.  }
  68. //--------------------------------------------------------------------------------
  69.  //查找并打印文件夹下指定图片,可以包含通配符 *, ?
  70. //@pdwCount - [out]找到的文件个数
  71.  BOOL PrintFolderImages(LPCTSTR sImgPath, LPCTSTR printer, UINT * pdwCount) 
  72.  {
  73.   CFileFind ff;
  74.   BOOL res=ff.FindFile(sImgPath);
  75.   UINT count(0);
  76.   while (res)
  77.   {
  78.    res=ff.FindNextFile();
  79.    count++;
  80.   }
  81.   if (0 == count)
  82.   {
  83.    if(NULL != pdwCount)
  84.     *pdwCount = count;
  85.    return TRUE;
  86.   }
  87.   TCHAR sParentFolder[MAX_PATH];
  88.   lstrcpy(sParentFolder,sImgPath);
  89.   GetParentFolder(sParentFolder);
  90.   FormatFolderName2(sParentFolder);
  91.   GetFileOnlyName(sParentFolder);
  92.   CPrintDialog printDlg(FALSE);
  93.   // Or get from user:
  94.   //INT_PTR result = printDlg.DoModal();
  95.   //if (result == IDCANCEL)
  96.   // return;
  97.   TCHAR pOldPrinter[MAX_PATH];
  98.   pOldPrinter[0] = TEXT('/0');
  99.   if (printer != NULL)
  100.   {
  101.    DWORD nSize(MAX_PATH);
  102.    if(!GetDefaultPrinter(pOldPrinter,&nSize))
  103.    {
  104.     pOldPrinter[0] = TEXT('/0');
  105.    }
  106.    if(!SetDefaultPrinter(printer))
  107.     return FALSE;
  108.   }
  109.   printDlg.GetDefaults();
  110.   if (printer != NULL && pOldPrinter[0] != TEXT('/0'))
  111.   {
  112.    SetDefaultPrinter(pOldPrinter);
  113.   }
  114.   CDC dc;
  115.   if (!dc.Attach(printDlg.GetPrinterDC())) 
  116.   {
  117.    //::MessageBox(NULL, TEXT("No printer found!"), TEXT("Error"), MB_OK|MB_ICONINFORMATION);
  118.    SetLastError(2150); //打印机不存在
  119.    return FALSE;
  120.   } 
  121.   dc.m_bPrinting = TRUE; 
  122.   DOCINFO di;    
  123.   // Initialise print document details
  124.   ::ZeroMemory (&di, sizeof (DOCINFO));
  125.   di.cbSize = sizeof (DOCINFO);
  126.   di.lpszDocName = sParentFolder; 
  127.   BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job 
  128.   // Get the printing extents
  129.   // and store in the m_rectDraw field of a 
  130.   // CPrintInfo object
  131.   CPrintInfo Info;
  132.   Info.SetMaxPage(count); // just one page 
  133.   float maxw = static_cast<float>(dc.GetDeviceCaps(HORZSIZE) / 0.254);
  134.   float maxh = static_cast<float>(dc.GetDeviceCaps(VERTSIZE) / 0.254);
  135.   Info.m_rectDraw.SetRect(0, 0, static_cast<int>(maxw), static_cast<int>(maxh));
  136.   RectF rectf(0, 0, maxw, maxh);
  137.   res=ff.FindFile(sImgPath);
  138.   count = 0;
  139.   for (UINT page = Info.GetMinPage(); page <= 
  140.    Info.GetMaxPage() && bPrintingOK && res; page++) 
  141.   {
  142.    res=ff.FindNextFile();
  143.    dc.StartPage();    // begin new page
  144.    Info.m_nCurPage = page;
  145.    {
  146.     Graphics graphics(dc.GetSafeHdc());
  147.     Image img(ff.GetFilePath());
  148.     graphics.DrawImage(&img, rectf, 0, 0, (REAL)img.GetWidth(), (REAL)img.GetHeight(), UnitPixel);
  149.    }
  150.    count++;
  151.    bPrintingOK = (dc.EndPage() > 0);   // end page
  152.   } 
  153.   if(NULL != pdwCount)
  154.    *pdwCount = count;
  155.   if (bPrintingOK)
  156.   {
  157.    dc.EndDoc(); // end a print job
  158.    return TRUE;
  159.   }
  160.   else 
  161.   {
  162.    dc.AbortDoc();           // abort job. 
  163.    return FALSE;
  164.   }
  165.  }

完整代码下载:http://download.csdn.net/source/948960

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值