Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to print a jpeg image from my VC++ program( through code). Any API's available for
direct image printing. Is it possible? Please help me to find a way.

Thanks,
Mithun
Posted

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.
 
Share this answer
 
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.


C++
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 print

	BOOL 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 page
		oInfo.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 context
		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);   // end page
	} 

	if (bPrintingOK)
		prtrDC.EndDoc(); // end a print job
	else 
		prtrDC.AbortDoc();// abort job. 

	SetDefaultPrinter(szPrinter);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900