Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
Bitmap *pbmp     //global variable

//trying to make a buffer for the constructor, but it doesn't work as expected
byte *bytes = new byte[1000];
	for (size_t i = 0; i < 1000; i++)
	{
		bytes[i] = 255;
	}
	Bitmap bmp(100, 100, 500, PixelFormat32bppARGB, bytes);


Here is how i'm creating a bitmap at runtime, and since I need to change little portions of it i'm using this function inside WM_PAINT :

C++
void LockBits()
{
	UINT* pixels;
	// Lock a 50xs30 rectangular portion of the bitmap for writing.
	BitmapData bitmapData;
	Rect rect(20, 10, 50, 60);
			
	pbmp->LockBits( &rect, ImageLockModeWrite, PixelFormat32bppARGB, &bitmapData);

	// Write to the temporary buffer provided by LockBits.
	pixels = (UINT*)bitmapData.Scan0;

	for (UINT row = 0; row < 60; ++row)
		for (UINT col = 0; col < 50; ++col)
		{
			pixels[row * bitmapData.Stride / 4 + col] = 0xff00ff00;
		}

	// Commit the changes, and unlock the 50x30 portion of the bitmap.  
	pbmp->UnlockBits(&bitmapData);
}


//********************************************************************************
case WM_PAINT:
{
    LockBits();
    Graphics grap(GetDC(hwnd));
    grap.DrawImage(&(*pbmp), 150, 100);
}
break;


As soon as I run the program, it works (display the changed bitmap) but immediately after "Myprogram.exe has triggered a breakpoint." and then points to the line inside WinMain -DispatchMessage(&msg);-


EDIT: It appears that I didn't gave a big enough buffer to the bitmap constructor, setting it to 40000 does the trick. But isn't it a way too big number for a 100x100 bitmap ? How should I calculate buffer size ? width * height * 4 (since pixel format is 32bits) ?
Posted
Updated 12-Dec-14 7:16am
v4
Comments
[no name] 17-Dec-14 15:01pm    
You seem to have answered your own question.

1 solution

100x100x4 = 40000, why you indeed need that much memory for it.

For bufsize, you often need to round the width up to 4 (but that should be in the documentation).
bufsize = ROUNDUP_TO_4(width) * height * Bpp;

where Bpp = Bytes per pixel, and ROUNDUP_TO_4 could be
#define ROUNDUP_TO_4(x) (((x)+3)&~3)
 
Share this answer
 

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