Solved

GDI Bitmap and DC

Posted on 2011-02-24
4
491 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
4 Comments
 
LVL 26

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 33

Accepted Solution

by:
sarabande earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
A short article about problems I had with the new location API and permissions in Marshmallow
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

773 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