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来绘制打印,完整代码如下:
- #include <atlbase.h>
- #include <Windows.h>
- #include <winspool.h>
- #include <gdiplus.h>
- #include <Gdiplusgraphics.h>
- #pragma comment(lib, "Winspool.lib")
- #pragma comment(lib, "gdiplus.lib")
- //打印一张图片:
- BOOL PrintImage(LPCTSTR filename)
- {
- if (!FileExist(filename))
- {
- SetLastError(2); //系统找不到指定的文件
- return FALSE;
- }
- CPrintDialog printDlg(FALSE);
- printDlg.GetDefaults();
- // Or get from user:
- //INT_PTR result = printDlg.DoModal();
- //if (result == IDCANCEL)
- // return;
- CDC dc;
- if (!dc.Attach(printDlg.GetPrinterDC()))
- {
- SetLastError(2150); //打印机不存在
- ::MessageBox(NULL, TEXT("打印机不存在"), TEXT("Error"), MB_OK|MB_ICONINFORMATION);
- return FALSE;
- }
- dc.m_bPrinting = TRUE;
- DOCINFO di;
- // Initialise print document details
- ::ZeroMemory (&di, sizeof (DOCINFO));
- di.cbSize = sizeof (DOCINFO);
- di.lpszDocName = filename;
- BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job
- // Get the printing extents
- // and store in the m_rectDraw field of a
- // CPrintInfo object
- CPrintInfo Info;
- Info.SetMaxPage(1); // just one page
- float maxw = static_cast<float>(dc.GetDeviceCaps(HORZSIZE) / 0.254);
- float maxh = static_cast<float>(dc.GetDeviceCaps(VERTSIZE) / 0.254);
- Info.m_rectDraw.SetRect(0, 0, static_cast<int>(maxw), static_cast<int>(maxh));
- RectF rectf(0, 0, maxw, maxh);
- for (UINT page = Info.GetMinPage(); page <=
- Info.GetMaxPage() && bPrintingOK; page++)
- {
- dc.StartPage(); // begin new page
- Info.m_nCurPage = page;
- {
- Graphics graphics(dc.GetSafeHdc());
- Image img(filename);
- graphics.DrawImage(&img, rectf, 0, 0, (REAL)img.GetWidth(), (REAL)img.GetHeight(), UnitPixel);
- }
- bPrintingOK = (dc.EndPage() > 0); // end page
- }
- if (bPrintingOK)
- {
- dc.EndDoc(); // end a print job
- return TRUE;
- }
- else
- {
- dc.AbortDoc(); // abort job.
- return FALSE;
- }
- }
- //--------------------------------------------------------------------------------
- //查找并打印文件夹下指定图片,可以包含通配符 *, ?
- //@pdwCount - [out]找到的文件个数
- BOOL PrintFolderImages(LPCTSTR sImgPath, LPCTSTR printer, UINT * pdwCount)
- {
- CFileFind ff;
- BOOL res=ff.FindFile(sImgPath);
- UINT count(0);
- while (res)
- {
- res=ff.FindNextFile();
- count++;
- }
- if (0 == count)
- {
- if(NULL != pdwCount)
- *pdwCount = count;
- return TRUE;
- }
- TCHAR sParentFolder[MAX_PATH];
- lstrcpy(sParentFolder,sImgPath);
- GetParentFolder(sParentFolder);
- FormatFolderName2(sParentFolder);
- GetFileOnlyName(sParentFolder);
- CPrintDialog printDlg(FALSE);
- // Or get from user:
- //INT_PTR result = printDlg.DoModal();
- //if (result == IDCANCEL)
- // return;
- TCHAR pOldPrinter[MAX_PATH];
- pOldPrinter[0] = TEXT('/0');
- if (printer != NULL)
- {
- DWORD nSize(MAX_PATH);
- if(!GetDefaultPrinter(pOldPrinter,&nSize))
- {
- pOldPrinter[0] = TEXT('/0');
- }
- if(!SetDefaultPrinter(printer))
- return FALSE;
- }
- printDlg.GetDefaults();
- if (printer != NULL && pOldPrinter[0] != TEXT('/0'))
- {
- SetDefaultPrinter(pOldPrinter);
- }
- CDC dc;
- if (!dc.Attach(printDlg.GetPrinterDC()))
- {
- //::MessageBox(NULL, TEXT("No printer found!"), TEXT("Error"), MB_OK|MB_ICONINFORMATION);
- SetLastError(2150); //打印机不存在
- return FALSE;
- }
- dc.m_bPrinting = TRUE;
- DOCINFO di;
- // Initialise print document details
- ::ZeroMemory (&di, sizeof (DOCINFO));
- di.cbSize = sizeof (DOCINFO);
- di.lpszDocName = sParentFolder;
- BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job
- // Get the printing extents
- // and store in the m_rectDraw field of a
- // CPrintInfo object
- CPrintInfo Info;
- Info.SetMaxPage(count); // just one page
- float maxw = static_cast<float>(dc.GetDeviceCaps(HORZSIZE) / 0.254);
- float maxh = static_cast<float>(dc.GetDeviceCaps(VERTSIZE) / 0.254);
- Info.m_rectDraw.SetRect(0, 0, static_cast<int>(maxw), static_cast<int>(maxh));
- RectF rectf(0, 0, maxw, maxh);
- res=ff.FindFile(sImgPath);
- count = 0;
- for (UINT page = Info.GetMinPage(); page <=
- Info.GetMaxPage() && bPrintingOK && res; page++)
- {
- res=ff.FindNextFile();
- dc.StartPage(); // begin new page
- Info.m_nCurPage = page;
- {
- Graphics graphics(dc.GetSafeHdc());
- Image img(ff.GetFilePath());
- graphics.DrawImage(&img, rectf, 0, 0, (REAL)img.GetWidth(), (REAL)img.GetHeight(), UnitPixel);
- }
- count++;
- bPrintingOK = (dc.EndPage() > 0); // end page
- }
- if(NULL != pdwCount)
- *pdwCount = count;
- if (bPrintingOK)
- {
- dc.EndDoc(); // end a print job
- return TRUE;
- }
- else
- {
- dc.AbortDoc(); // abort job.
- return FALSE;
- }
- }
完整代码下载:http://download.csdn.net/source/948960