本文介绍了VC的JPEG图像打印。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想从VC ++程序(通过代码)打印jpeg图像。任何API都可用于 直接图像打印。可能吗?请帮我找个方法。 谢谢, Mithun 解决方案 除了使用不同的设备上下文之外,这与在Window中显示它相同。通过Google,从C ++和C ++ / CLI打印时,有很多文章可以找到。 感谢理查德的建议,我已经完成了。在这里,我发布了我的工作代码。 我把我的打印机DC和绘制的图像放在DC上。 void CPrint_JpegDlg :: PrintJpegImage() { TCHAR szPrinter [_MAX_PATH]; DWORD cntChars = _MAX_PATH; GetDefaultPrinter(szPrinter,& cntChars); SetDefaultPrinter(L \\\\ quotinter name); CPrintDialog printDlg(FALSE); printDlg.GetDefaults(); CDC prtrDC; if (!prtrDC.Attach(printDlg.GetPrinterDC())) { AfxMessageBox(_T( 找不到打印机!)); return ; } prtrDC.m_bPrinting = TRUE; DOCINFO imageInfo; :: ZeroMemory(& imageInfo, sizeof (DOCINFO)); imageInfo.cbSize = sizeof (DOCINFO); imageInfo.lpszDocName = L C:\\test.jpg; // 要打印的文件名 BOOL bPrintingOK = prtrDC.StartDoc(& ;的imageinfo); CPrintInfo oInfo; oInfo.SetMaxPage( 1 ); // 只有一页 int nMaxWidth = prtrDC.GetDeviceCaps(HORZRES); int nMaxHeight = prtrDC.GetDeviceCaps(VERTRES); oInfo.m_rectDraw.SetRect( 0 , 0 ,nMaxWidth,nMaxHeight) ; for (UINT page = oInfo.GetMinPage(); page< = oInfo.GetMaxPage()&& bPrintingOK; page ++) { prtrDC.StartPage(); // 开始新页 oInfo.m_nCurPage = page; CBitmap位图; if (!bitmap.Attach(:: LoadImage(:: GetModuleHandle(NULL),L C:\\test.jpg,IMAGE_BITMAP, 0 , 0 , LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE))) { AfxMessageBox(_T( 加载位图时出错!)); return ; } BITMAP bm; bitmap.GetBitmap(& bm); int nWidth = bm.bmWidth; int nHeight = bm.bmHeight; // 创建内存设备上下文 CDC memDC; memDC.CreateCompatibleDC(& prtrDC); CBitmap * pBmp = memDC.SelectObject(& bitmap); memDC.SetMapMode(prtrDC.GetMapMode()); prtrDC.SetStretchBltMode(HALFTONE); prtrDC.StretchBlt( 0 , 0 ,nMaxWidth,nMaxHeight,& memDC, 0 , 0 ,nWidth,nHeight,SRCCOPY); memDC.SelectObject(pBmp); bPrintingOK =(prtrDC.EndPage()> 0 ); // 结束页 } if (bPrintingOK) prtrDC.EndDoc(); // 结束打印作业 else prtrDC.AbortDoc(); // 中止作业。 SetDefaultPrinter(szPrinter); } Hi,I want to print a jpeg image from my VC++ program( through code). Any API''s available fordirect image printing. Is it possible? Please help me to find a way.Thanks,Mithun 解决方案 This is just the same as displaying it in a Window, except you use a different device context. There are plenty of articles to be found, via Google, on printing from C++ and C++/CLI.Thanks Richard for the suggestion, I have done it. Here I am posting mine worked code.I took my printers DC and drawn image on that DC.void CPrint_JpegDlg::PrintJpegImage() {TCHAR szPrinter[_MAX_PATH];DWORD cntChars = _MAX_PATH;GetDefaultPrinter(szPrinter, &cntChars);SetDefaultPrinter(L"\\printer name");CPrintDialog printDlg(FALSE);printDlg.GetDefaults(); CDC prtrDC;if (!prtrDC.Attach(printDlg.GetPrinterDC())){AfxMessageBox(_T("No printer found!"));return;} prtrDC.m_bPrinting = TRUE; DOCINFO imageInfo;::ZeroMemory (&imageInfo, sizeof (DOCINFO));imageInfo.cbSize = sizeof (DOCINFO);imageInfo.lpszDocName = L"C:\\test.jpg"; //filename to be printBOOL bPrintingOK = prtrDC.StartDoc(&imageInfo); CPrintInfo oInfo;oInfo.SetMaxPage(1); // just one page int nMaxWidth = prtrDC.GetDeviceCaps(HORZRES);int nMaxHeight = prtrDC.GetDeviceCaps(VERTRES); oInfo.m_rectDraw.SetRect(0, 0, nMaxWidth, nMaxHeight); for (UINT page = oInfo.GetMinPage(); page <= oInfo.GetMaxPage() && bPrintingOK; page++) {prtrDC.StartPage(); // begin new pageoInfo.m_nCurPage = page;CBitmap bitmap;if(!bitmap.Attach(::LoadImage( ::GetModuleHandle(NULL), L"C:\\test.jpg", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE))) {AfxMessageBox(_T("Error loading bitmap!"));return;} BITMAP bm;bitmap.GetBitmap(&bm);int nWidth = bm.bmWidth; int nHeight = bm.bmHeight; // create memory device contextCDC memDC; memDC.CreateCompatibleDC(&prtrDC);CBitmap *pBmp = memDC.SelectObject(&bitmap);memDC.SetMapMode(prtrDC.GetMapMode());prtrDC.SetStretchBltMode(HALFTONE);prtrDC.StretchBlt(0, 0, nMaxWidth, nMaxHeight, &memDC, 0, 0, nWidth, nHeight, SRCCOPY); memDC.SelectObject(pBmp);bPrintingOK = (prtrDC.EndPage() > 0); // end page} if (bPrintingOK)prtrDC.EndDoc(); // end a print jobelse prtrDC.AbortDoc();// abort job. SetDefaultPrinter(szPrinter);} 这篇关于VC的JPEG图像打印。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 03:02