?
Solved

GDI Bitmap and DC

Posted on 2011-02-24
4
Medium Priority
?
505 Views
Last Modified: 2013-11-20
Hello,

I have a difficult problem. In the past I used an Object, set in the Header a as member
like: myObject Object;
Now I changed this to a dynamic object, where I have myObject* Object; and I generate it wtih "new". In the past I submit a DC to the Object with SetPreviewDC(xxx.GetDC()->GetSafeHdc());
Everything worked well, but now after generate the object dynamicly, it fail. The hBmp is allways 0. Anybody can help here?

Ocrana

HBITMAP VideoSequence::GenerateDCPicture(HDC hDC)
{
	HBITMAP hBMP = 0;

	// if not skipped create bitmap from I_FRAME
	if(!skipped)
	{
		int target_width  = Frame.target_width;
		int target_height = Frame.target_height;
		int padding_32b = (4 - (target_width * 3) % 4) % 4;

		BITMAPINFO targetBmi;
		BITMAPINFO sourceBmi;

		ZeroMemory(&targetBmi.bmiHeader, sizeof(BITMAPINFOHEADER));
		targetBmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		targetBmi.bmiHeader.biWidth = target_width;
		targetBmi.bmiHeader.biHeight = -(target_height);
		targetBmi.bmiHeader.biPlanes = 1;
		targetBmi.bmiHeader.biBitCount = 24;
		targetBmi.bmiHeader.biCompression = BI_RGB;
		targetBmi.bmiHeader.biSizeImage = target_width * target_height * 3 + target_height * padding_32b;
		targetBmi.bmiHeader.biXPelsPerMeter = 72;
		targetBmi.bmiHeader.biYPelsPerMeter = 72;

		ZeroMemory(&sourceBmi.bmiHeader, sizeof(BITMAPINFOHEADER));
		sourceBmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		sourceBmi.bmiHeader.biWidth = target_width;
		sourceBmi.bmiHeader.biHeight = -(target_height);
		sourceBmi.bmiHeader.biPlanes = 1;
		sourceBmi.bmiHeader.biBitCount = 24;
		sourceBmi.bmiHeader.biCompression = BI_RGB;
		sourceBmi.bmiHeader.biSizeImage = target_width * target_height * 3 + target_height * padding_32b;
		sourceBmi.bmiHeader.biXPelsPerMeter = 72;
		sourceBmi.bmiHeader.biYPelsPerMeter = 72;

		unsigned char *buf = Frame.GetDCPicture();


		hBMP = CreateDIBitmap(hDC, (BITMAPINFOHEADER *)&targetBmi.bmiHeader, CBM_INIT, buf, &sourceBmi, DIB_RGB_COLORS);

		if(hBMP == 0)
		{
			LPVOID lpMsgBuf;
			FormatMessage( 
				FORMAT_MESSAGE_ALLOCATE_BUFFER | 
				FORMAT_MESSAGE_FROM_SYSTEM | 
				FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL,
				GetLastError(),
				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				(LPTSTR) &lpMsgBuf,
				0,
				NULL 
			);
			// Process any inserts in lpMsgBuf.
			// ...
			// Display the string.
			MessageBox( NULL, (LPCTSTR)lpMsgBuf, _T("Error"), MB_OK | MB_ICONINFORMATION );
			// Free the buffer.
			LocalFree( lpMsgBuf );
		}
	}
	else
	{
		hBMP = (HBITMAP)CopyImage(hHyperSpeedBMP, IMAGE_BITMAP, 0, 0, 0);
	}

	return hBMP;
}

Open in new window

0
Comment
Question by:Ocrana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 28

Expert Comment

by:dpearson
ID: 34976673
Hey Ocrana,

I think you need to show us where this allocation is happening and where the code is that you changed.

I don't see any calls to "new" happening in here.

In general switching from
myObject Object;
to
myObject* Object = new myObject() ;

shouldn't make any difference - you're just allocating the object on the heap instead of the stack.  The classic mistakes are around:
1) Never deleting the object (which will produce a memory leaking)
2) Or deleting it twice (which corrupts the free memory list for the heap and will crash your app later).

Neither should cause hBMP to be 0 - unless you're running out of available heap space?

Anyway best to post the code where you made the change to the allocation.

Doug
0
 
LVL 35

Accepted Solution

by:
sarabande earned 1500 total points
ID: 34977520
check the return of xxx.GetDC()->GetSafeHdc();


the xxx must be a valid CWnd, the one where you want to draw the bitmap into. you better would call the GetSafeHdc at a time where you already were drawing and where the devive context was valid therefore.

Sara
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34980414
>>      if(!skipped)


Have you checked your code block to allocate the bitmap is actually being run?  Put a breakpoint in it.
0
 

Author Closing Comment

by:Ocrana
ID: 35123378
It was a good way to the solution but not the solution itself.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Simple Linear Regression

764 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