Jump to content
  • Advertisement
Sign in to follow this  

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();

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

	//Create a DIB section of this bitmap
    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;
	void* bits;
	HBITMAP dibsection = CreateDIBSection(DrawmeDC.m_hDC, &bitmapinfo, DIB_RGB_COLORS, &bits, NULL, 0);
	CBitmap DrawMeBmp;
        //Select this bitmap into a DC
	CBitmap* pOldBitmap = SourceDC.SelectObject(&DrawMeBmp);

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

	//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...

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
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!