Click here to Skip to main content
14,243,034 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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 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);
}
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100