Help creating a bitmap from pixel data

I've spent some time looking through information about bitmaps, but it all seems either much more advanced than what I'm trying to do or it focuses on files. I have found an example below that works just fine for loading a bitmap from a file. The problem is that I want to replace the step where the image is loaded from a file with simply dumping my array of pixel data. I have a stream of RGBARGBARGBA 32-bit pixel data. I know the width, height, and 32-bit color data for each pixel. What I don't know is the best way to put that information into a bitmap that Windows can draw on the screen.

In the code below, I want to replace the following line with my pixel data:

HBITMAP cross = (HBITMAP)LoadImage(NULL, _T("E:\\cross.bmp") ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

Instead of LoadImage, I want to create an image with W, H, PixelData

Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>
#include <tchar.h>

int main()
{
	HDC hdc = CreateCompatibleDC(NULL);
	HBITMAP cross = (HBITMAP)LoadImage(NULL, _T("E:\\cross.bmp") ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	SelectObject(hdc, cross);
	while (1)
	{
		HDC hdc_x = GetDC(HWND_DESKTOP);
		BitBlt(hdc_x,488,359,48,48,hdc,0,0,SRCCOPY);	
		ReleaseDC(HWND_DESKTOP,hdc_x);
	}

	return 0;
}
Last edited on
I'm not sure what exactly you want. Maybe CreateDIBSection(...). You can manipulate the pixels directly and select it into the HDC.
Why don't you draw the pixels yourself instead of creating a bitmap ?
You can do it with SetPixel.
https://msdn.microsoft.com/de-de/library/windows/desktop/dd145078(v=vs.85).aspx
Do not reinvent the wheel. Use the fantastic bitmap library already created.
All you nee do is add an include file during compilation and you have the functions necessary to deal with bitmaps

http://www.partow.net/programming/bitmap/index.html
Hey, this website seems very good. Thanks for sharing.
Topic archived. No new replies allowed.