Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CreateDIBSection problem (OpenGL-related)

Posted on 2009-02-23
2
Medium Priority
?
1,226 Views
Last Modified: 2013-12-06
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
Comment
Question by:thready
2 Comments
 
LVL 11

Accepted Solution

by:
jgordos earned 2000 total points
ID: 23714106
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
 
LVL 1

Author Closing Comment

by:thready
ID: 31550204
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question