Solved

GDI Bitmap and DC

Posted on 2011-02-24
4
504 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 27

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 34

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

705 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