Solved

GDI Bitmap and DC

Posted on 2011-02-24
4
480 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 32

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now