Click here to Skip to main content
15,888,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is how i'm creating the bitmap
C++
Bitmap bmp(100, 100, PixelFormat32bppARGB);


Then I use LockBits to change a portion of the bitmap's pixels

C++
void Example_LockBits2()
{
	UINT* pixels;
	// Lock a 50xs30 rectangular portion of the bitmap for writing.
	BitmapData bitmapData;
	Rect rect(20, 10, 50, 30);

	bmp.LockBits(
		&rect,
		ImageLockModeWrite,
		PixelFormat32bppARGB,
		&bitmapData);

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

	for (UINT row = 0; row < 30; ++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.  
	bmp.UnlockBits(&bitmapData);
}


And finally try to display it in WM_PAINT

C++
case WM_PAINT:
	{
		Example_LockBits2();
		Graphics grap(GetDC(hwnd));
		grap.DrawImage(&bmp, 150, 100);
		break;
	}



Since i'm new to GDI+ i think it could be anything, especially on the creation of the Bitmap, any help appreciated
Posted
Comments
Richard MacCutchan 12-Dec-14 10:02am    
What happens if you try without calling Example_LockBits2? Does it then work successfully? You also need to check that your call to DrawImage conforms to the rules in the documentation.
Member 11287295 12-Dec-14 11:23am    
Doesn't work either way, that's why I'm thinking you cannot create a bitmap with that constructor, but I can't see no example online on how to create a gdi+ bitmap on runtime
Richard MacCutchan 12-Dec-14 11:35am    
I have:

Graphics myGraphics(GetDC(hWnd));
PointF point(0., 0.);
Image* image = new Image(L"BACK12.BMP");
myGraphics.DrawImage(image, point);

which works fine. You need to use your debugger to find out what is happening in your code.
Member 11287295 12-Dec-14 11:49am    
I assume you're loading a picture from a file, which is not what i'm trying to do. I need to create variable in size bitmaps on runtime
Richard MacCutchan 12-Dec-14 11:57am    
You have not shown where your bitmap comes from or how you created it. I would suggest you focus on getting a basic bitmap displayed, before trying to modify it. See http://msdn.microsoft.com/en-us/library/ms533815(v=vs.85).aspx for more information.

1 solution

You need to assign bitmapData to the return value for Lockbits.

BitmapData bitmapData = bmp.LockBits(
    &rect,
    ImageLockModeWrite,
    PixelFormat32bppARGB,
    &bitmapData);
 
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