Jump to content
  • Advertisement
Sign in to follow this  
Scint

Printing a bitmap in MFC

This topic is 4444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently writing an application that requires me to print a bitmap to a printer. While I have gotten rendering to work fine in the window, I am having some problems printing to a printer. I can do stuff like line drawing, rectangles and the like, but not bitmaps. My current approach has been to draw to a bitmap, then create a DIB section and call StretchDIBits() using the printer's DC (in OnPrint()). Here is my code from OnPrint:
CPGSkinsDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);	

	//Get the bitmap to be drawn
	CBitmap* pSourceBMP = pDoc->m_MyBmp.GetRotatedBmp();

	//Create a DIB section of this bitmap
	DIBSECTION ds;
    pSourceBMP->GetObject( sizeof(DIBSECTION), &ds );

	//fill in the BITMAPINFO with information from the DIB section
	BITMAPINFO bitmapinfo;
    ZeroMemory(&bitmapinfo, sizeof(bitmapinfo));
    bitmapinfo.bmiHeader.biSize = sizeof(bitmapinfo.bmiHeader);
	bitmapinfo.bmiHeader.biWidth = ds.dsBm.bmWidth;
	bitmapinfo.bmiHeader.biHeight = ds.dsBm.bmHeight;
	bitmapinfo.bmiHeader.biPlanes = ds.dsBm.bmPlanes;
    bitmapinfo.bmiHeader.biBitCount = ds.dsBm.bmBitsPixel;
    bitmapinfo.bmiHeader.biCompression = BI_RGB;

	int width, height;
	width = ds.dsBm.bmWidth;
	height = ds.dsBm.bmHeight;

	
	CDC DrawmeDC;
	CDC SourceDC;
	DrawmeDC.CreateCompatibleDC(pDC);
	SourceDC.CreateCompatibleDC(pDC);
	
	void* bits;
	HBITMAP dibsection = CreateDIBSection(DrawmeDC.m_hDC, &bitmapinfo, DIB_RGB_COLORS, &bits, NULL, 0);
	
	CBitmap DrawMeBmp;
	DrawMeBmp.Attach(dibsection);
	
        //Select this bitmap into a DC
	CBitmap* pOldBitmap = SourceDC.SelectObject(&DrawMeBmp);

	DrawmeDC.BitBlt(0,0, width, height, &SourceDC, 0, 0, SRCCOPY );

	pDC->SetMapMode(MM_LOENGLISH); 
		
	//Stretch DIB
	//letter paper
	int rval = StretchDIBits(pDC->m_hDC, 0, 0, 850, -1100, 0, 0, width,    height, 
		bits, &bitmapinfo, DIB_RGB_COLORS, SRCCOPY);

	if( rval == GDI_ERROR ) {
		TRACE( _T("Something wrong in StretchDIBits().\n") );
	}

        //clean up...
	SourceDC.SelectObject(pOldBitmap);
	SourceDC.DeleteDC();
	DrawmeDC.DeleteDC();
	DrawMeBmp.DeleteObject();

When I do this, there is no GDI error, and neither my bitmap handle or bits pointer are NULL. If instead of doing a BitBlt I do stuff like LineTo or Rectangle, stuff does get drawn. But this code just makes a big black area.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!