• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1285
  • Last Modified:

CreateDIBSection problem (OpenGL-related)

Hi Experts,

I'd like to use CreateDIBSection with a memory DC so that I can draw to a bitmap with OpenGL directly, without ever showing it to the screen.  Is this possible?  Please see the code I have below - which returns NULL on my call to CreateDIBSection.

Thank you!
Mike
void COpenGLGraphPrintingTestDlg::OnBnClickedPrint()
{
	CSize szDIB;
	szDIB.cx = 14400* 6;
	szDIB.cy = 14400* 3;
	BITMAPINFO bmi;
	memset(&bmi, 0, sizeof(BITMAPINFO));
	bmi.bmiHeader.biSize			= sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth			= szDIB.cx;
	bmi.bmiHeader.biHeight		= szDIB.cy;
	bmi.bmiHeader.biPlanes		= 1;
	bmi.bmiHeader.biBitCount		= 24;
	bmi.bmiHeader.biCompression	= BI_RGB;
	bmi.bmiHeader.biSizeImage		= szDIB.cx * szDIB.cy * 3;
 
	LPVOID pBitmapBits;
 
	// Create DIB
	HDC	hDC = ::CreateCompatibleDC(NULL);
 
       // THIS CALL RETURNS NULL.............
	HBITMAP hDib = ::CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &pBitmapBits, NULL, (DWORD)0);
 
	if(!hDib)
	{
		DWORD dwResult = ::GetLastError();
		if(dwResult == ERROR_INVALID_PARAMETER)
			dwResult = dwResult;
	}
 
	// Setup memory DC's pixel format.
	if (!SetDCPixelFormat(hDC, PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_STEREO_DONTCARE))
	{
		DeleteObject(hDib);
		hDib = NULL;
		DeleteDC(hDC);
		hDC = NULL;
		return;
	}
 
	// Create memory DC
	HGLRC hMemRC = ::wglCreateContext(hDC);
 
	::wglMakeCurrent(hDC, hMemRC);
 
      RenderScene();
}
 
bool COpenGLGraphPrintingTestDlg::SetDCPixelFormat(HDC hDC, DWORD dwFlags)
{
	PIXELFORMATDESCRIPTOR pixelDesc;
	
	pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pixelDesc.nVersion = 1;
	
	pixelDesc.dwFlags = dwFlags;
	pixelDesc.iPixelType = PFD_TYPE_RGBA;
	pixelDesc.cColorBits = 24;
	pixelDesc.cRedBits = 8;
	pixelDesc.cRedShift = 16;
	pixelDesc.cGreenBits = 8;
	pixelDesc.cGreenShift = 8;
	pixelDesc.cBlueBits = 8;
	pixelDesc.cBlueShift = 0;
	pixelDesc.cAlphaBits = 0;
	pixelDesc.cAlphaShift = 0;
	pixelDesc.cAccumBits = 64;
	pixelDesc.cAccumRedBits = 16;
	pixelDesc.cAccumGreenBits = 16;
	pixelDesc.cAccumBlueBits = 16;
	pixelDesc.cAccumAlphaBits = 0;
	pixelDesc.cDepthBits = 32;
	pixelDesc.cStencilBits = 8;
	pixelDesc.cAuxBuffers = 0;
	pixelDesc.iLayerType = PFD_MAIN_PLANE;
	pixelDesc.bReserved = 0;
	pixelDesc.dwLayerMask = 0;
	pixelDesc.dwVisibleMask = 0;
	pixelDesc.dwDamageMask = 0;
	
	int nPixelIndex = ::ChoosePixelFormat(hDC, &pixelDesc);
	if (nPixelIndex == 0) // Choose default
	{
		nPixelIndex = 1;
		if (::DescribePixelFormat(hDC, nPixelIndex, 
			sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc) == 0)
			return false;
	}
 
	if (!::SetPixelFormat(hDC, nPixelIndex, &pixelDesc))
		return false;
 
	return true;
}

Open in new window

0
thready
Asked:
thready
1 Solution
 
jgordosCommented:
You're passing NULL as the HDC.. but you created your memDC  the line above...

But I think the problem might be that you're asking for a bitmap that is

      szDIB.cx = 14400* 6;
      szDIB.cy = 14400* 3;


86,400 x 43,200 and 24 bits in depth.

You're asking for a 3.7 gigabyte in memory bitmap....

I don't know of any of the old Win32 calls that can handle a bitmap that large.


To test, try the same code asking for something more reasonable... say 800 x 600, and see if it works...


-john
0
 
threadyAuthor Commented:
I didn't realize how much memory I was asking for.  I wanted it large so it would scale the MM_TWIPS printer DC mode.  I guess stretching my bitmap won't be so bad...  Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now