Solved

GDI Bitmap and DC

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

Independent Software Vendors: 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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

726 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