How to DrawText to a HBITMAP?

Started by
7 comments, last by CodaKiller 15 years, 4 months ago
How do I draw text to a DIBSection? Here is what I got so far but it just draws to the screen:

			BITMAPINFO bmi;
			BYTE *pBits;
			HBITMAP hBitmap;

			bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
			bmi.bmiHeader.biWidth = 300;
			bmi.bmiHeader.biHeight = 300;
			bmi.bmiHeader.biPlanes = 1;
			bmi.bmiHeader.biBitCount = 32;
			bmi.bmiHeader.biCompression = BI_RGB;
			bmi.bmiHeader.biSizeImage = 0;
			bmi.bmiHeader.biXPelsPerMeter = 0;
			bmi.bmiHeader.biYPelsPerMeter = 0;
			bmi.bmiHeader.biClrUsed = 0;
			bmi.bmiHeader.biClrImportant = 0;
			hBitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (VOID**)&pBits, NULL, 0); 

			RECT rect;
			rect.left = 0;
			rect.top = 0;
			rect.bottom = 100;
			rect.right = 100;
			HDC hDC = GetDC(hWnd);
			HFONT NewFont = CreateFont( 25, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial" );
			HBRUSH NewBrush = CreateSolidBrush(RGB(255, 0, 0));
			SelectObject(hDC, NewFont);
			SelectObject(hDC, NewBrush);
			DrawText(hDC, "See?", 4, &rect, DT_LEFT | DT_WORDBREAK);
			DeleteObject(NewBrush);
			DeleteObject(NewFont);
			ReleaseDC(hWnd, hDC);


[Edited by - CodaKiller on December 1, 2008 8:15:30 AM]
Remember Codeka is my alternate account, just remember that!
Advertisement
You need to select the bitmap into the dc.
-Mike
Quote:Original post by Anon Mike
You need to select the bitmap into the dc.


how?
Remember Codeka is my alternate account, just remember that!
Quote:Original post by CodaKiller
Quote:Original post by Anon Mike
You need to select the bitmap into the dc.


how?
After your GetDC() call:
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBitmap);

Then before ReleaseDC():
SelectObject(hDC, hBmpOld);

Or, use CreateCompatibleDC(NULL) to create a DC then select the bitmap into it at creation time.
Quote:Original post by Evil Steve
Quote:Original post by CodaKiller
Quote:Original post by Anon Mike
You need to select the bitmap into the dc.


how?
After your GetDC() call:
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBitmap);

Then before ReleaseDC():
SelectObject(hDC, hBmpOld);

Or, use CreateCompatibleDC(NULL) to create a DC then select the bitmap into it at creation time.


Here is what I'm doing and I think it works but I don't know since I still need to copy the data to a texture and display it:

			HDC hDC = CreateCompatibleDC(NULL);			DWORD* pSrcData = 0;			BITMAPINFO bmi = { sizeof( BITMAPINFOHEADER ), 256, 256, 1, 32, BI_RGB, 0, 0, 0, 0, 0};			HBITMAP hTempBmp = CreateDIBSection( hDC, &bmi, DIB_RGB_COLORS, (void**)&pSrcData, NULL, 0 );			RECT rect;			rect.left = 0;			rect.top = 0;			rect.bottom = 100;			rect.right = 100;			HFONT NewFont = CreateFont( 25, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial" );			HBRUSH NewBrush = CreateSolidBrush(RGB(255, 0, 0));			SelectObject(hDC, NewFont);			SelectObject(hDC, NewBrush);			DrawText(hDC, "See?", 4, &rect, DT_LEFT | DT_WORDBREAK);			DeleteObject(NewBrush);			DeleteObject(NewFont);			ReleaseDC(NULL, hDC);



Thank you very much, now all I need to know is how to get the raw data from it?

EDIT: I think I figured it out, I think it updates pSrcData after I draw to it am I right?
Remember Codeka is my alternate account, just remember that!
Quote:Original post by CodaKiller
Thank you very much, now all I need to know is how to get the raw data from it?
It's in pSrcData after you've drawn to the bitmap, and after you've called GdiFlush().
Quote:Original post by Evil Steve
Quote:Original post by CodaKiller
Thank you very much, now all I need to know is how to get the raw data from it?
It's in pSrcData after you've drawn to the bitmap, and after you've called GdiFlush().


Though I read it and I kinda understand it I still am not quite sure what GdiFlush exactly?
Remember Codeka is my alternate account, just remember that!
Quote:Original post by CodaKiller
Though I read it and I kinda understand it I still am not quite sure what GdiFlush exactly?
GDI is free to queue up several draw calls before actually doing any drawing, which can be better for performance. On Vista, all (?) GDI calls go through Direct3D surfaces, and locking a surface is fairly expensive - so it would be better for performance for GDI to lock the surface, draw 10 things, then unlock it instead of locking and unlocking for each individual draw command.
GdiFlush() just tells the GDI to render everything it has queued up now, so that the bitmap contains the data you expect.

The default batch size is probably 1 anyway, but it's always best not to assume these sorts of things.
Quote:Original post by Evil Steve
Quote:Original post by CodaKiller
Though I read it and I kinda understand it I still am not quite sure what GdiFlush exactly?
GDI is free to queue up several draw calls before actually doing any drawing, which can be better for performance. On Vista, all (?) GDI calls go through Direct3D surfaces, and locking a surface is fairly expensive - so it would be better for performance for GDI to lock the surface, draw 10 things, then unlock it instead of locking and unlocking for each individual draw command.
GdiFlush() just tells the GDI to render everything it has queued up now, so that the bitmap contains the data you expect.

The default batch size is probably 1 anyway, but it's always best not to assume these sorts of things.


You sir are a very smart man! Thanks!
Remember Codeka is my alternate account, just remember that!

This topic is closed to new replies.

Advertisement