Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
how can i draw into byte buffer (pImgPointer) of an image, at the moment i use this code but it's not work.
I can't use any MFC class only native code.

<br />
tagBITMAP tagBmp;<br />
	tagBmp.bmBits = pImgPointer;<br />
	tagBmp.bmBitsPixel = 32;<br />
	tagBmp.bmHeight = 1024;<br />
	tagBmp.bmPlanes = 1;<br />
	tagBmp.bmType = 0;<br />
	tagBmp.bmWidth = 1024;<br />
	tagBmp.bmWidthBytes = 4096;<br />
	memset(pImgPointer,0xff,sizeof(unsigned char)*tagBmp.bmWidth*tagBmp.bmWidthBytes);<br />
<br />
	HBITMAP hbmp = CreateBitmapIndirect(&tagBmp);<br />
<br />
	HDC dc1 = GetDC(NULL);<br />
	HDC memDc1 = CreateCompatibleDC(dc1);<br />
	HBITMAP oldHBmp = (HBITMAP)SelectObject(memDc1,hbmp);<br />
	HBRUSH radar = CreateSolidBrush(colorLkUp.getColor(true,0));<br />
	HBRUSH oldBrush = (HBRUSH)SelectObject(memDc1,radar);<br />
<br />
	Rectangle(memDc1,0,0,340,560);<br />
<br />
	hbmp = (HBITMAP)SelectObject(memDc1,oldHBmp);<br />
	SelectObject(memDc1,oldBrush);<br />
	DeleteObject(hBitmap);<br />
	DeleteObject(radar);<br />
	DeleteDC(memDc1);<br />
	DeleteDC(dc1);<br />


thank you in advanced for any answer
Posted

1 solution

What part of your code doesn't work? Do you have a resource leak? Rectangle drawn too small? Nothing drawn at all? Program exits with an unhandled exception? Have you checked all return values? What did they give? Called GetLastError() on failure? ...

Your calculations are off (bmWidth * bmWidthBytes) should be (bmHeight * bmWidthBytes) Doesn't matter when they have the same value, but still.

Why not use CreateCompatibleBitmap[^] instead?

From MSDN "An application must not delete a DC whose handle was obtained by calling the GetDC function. Instead, it must call the ReleaseDC function to free the DC."
 
Share this answer
 
v2
Comments
Ruscoff 17-Sep-10 9:52am    
Nothing drawn at all, all the image remain all white.
The image is a square, so width equal to heigth. I checked with the memory window and seems that the code never change bytes of buffer, such as the image isn't link to HDC.
Niklas L 17-Sep-10 10:42am    
You cannot check the pImgPointer buffer, since it won't change. It is used to initialize the bitmap. After you select your bitmap out of the memory device context, try blitting it to the screen, and you will see how it looks. If you need your bitmap bits, you will have to query the HBITMAP for them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900