Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Creating a bitmap from a DC

627 views
Skip to first unread message

Todd Aspeotis

unread,
Jul 21, 2005, 3:59:06 AM7/21/05
to
Now that Mike Sutton has kindly given me a solution for my layered window
problem, I now need to implement it. I'm fine for everything but getting the
text from DrawText onto a bitmap. I'm thinking something along the lines of

HDC hAlpha = CreateCompatibleDC(NULL);
SelectObject(hAlpha, myFont);
SetTextColour(hAlpha, RGB(255, 255, 255));
DrawText(hAlpha, TEXT("STORM System v.1")....);
// Use GetTextExtents32 or whatever function to get width & height of text
// create 8 bit bitmap with width & height that of the text cx cy
// copy from DC to bitmap
DeleteDC(hAlpha);

I'm all good up until Copy from DC to bitmap. Equally awesome would be if
you could create a DIB directly from a DC.

Thanks,

Todd

Mike D Sutton

unread,
Jul 21, 2005, 7:33:26 PM7/21/05
to

When you create a DC it initially has default 'stock' objects selected into it, including the stock 1*1*1 Bitmap. Since
there is a Bitmap already selected into the DC when you call DrawText() it will still try and render to it even though
pretty much everything (apart from one pixel) will be clipped.
What you need to do is to create a Bitmap, either DDB or DIBSection, and select that into your DC before drawing to it.
First though you need to find the size of your Bitmap since you want it large enough to display your text in, so for
that you use the DrawText() call again on the initial DC but include the DT_CALCRECT flag. What this does is rather
than drawing anything it simply measures how large the text is and dumps that into the RECT you pass the call.
From here you can go ahead and create your DIBSection using those dimensions and select it into your DC. Finally
perform your existing DrawText ()call (you may also want to use SetBkMode/Color()) which will render the text to the
DIBSection from which you can get at the data.
This seems to work pretty well here:

'***
HBITMAP CreateAlphaTextBitmap(LPCSTR inText, HFONT inFont, COLORREF inColour) {
int TextLength = (int)strlen(inText);
if (TextLength <= 0) return NULL;

// Create DC and select font into it
HDC hTextDC = CreateCompatibleDC(NULL);
HFONT hOldFont = (HFONT)SelectObject(hTextDC, inFont);
HBITMAP hMyDIB = NULL;

// Get text area
RECT TextArea = {0, 0, 0, 0};
DrawText(hTextDC, inText, TextLength, &TextArea, DT_CALCRECT);

if ((TextArea.right > TextArea.left) && (TextArea.bottom > TextArea.top)) {
BITMAPINFOHEADER BMIH;
memset(&BMIH, 0x0, sizeof(BITMAPINFOHEADER));

void *pvBits = NULL;

// Specify DIB setup
BMIH.biSize = sizeof(BMIH);
BMIH.biWidth = TextArea.right - TextArea.left;
BMIH.biHeight = TextArea.bottom - TextArea.top;
BMIH.biPlanes = 1;
BMIH.biBitCount = 32;
BMIH.biCompression = BI_RGB;

// Create and select DIB into DC
hMyDIB = CreateDIBSection(hTextDC, (LPBITMAPINFO)&BMIH, 0, (LPVOID*)&pvBits, NULL, 0);
HBITMAP hOldBMP = (HBITMAP)SelectObject(hTextDC, hMyDIB);

if (hOldBMP != NULL) {
// Set up DC properties
SetTextColor(hTextDC, 0x00FFFFFF);
SetBkColor(hTextDC, 0x00000000);
SetBkMode(hTextDC, OPAQUE);

// Draw text to buffer
DrawText(hTextDC, inText, TextLength, &TextArea, DT_NOCLIP);

BYTE* DataPtr = (BYTE*)pvBits;
BYTE FillR = GetRValue(inColour);
BYTE FillG = GetGValue(inColour);
BYTE FillB = GetBValue(inColour);
BYTE ThisA;

for (int LoopY = 0; LoopY < BMIH.biHeight; LoopY++) {
for (int LoopX = 0; LoopX < BMIH.biWidth; LoopX++) {
ThisA = *DataPtr; // Move alpha and pre-multiply with RGB
*DataPtr++ = (FillB * ThisA) >> 8;
*DataPtr++ = (FillG * ThisA) >> 8;
*DataPtr++ = (FillR * ThisA) >> 8;
*DataPtr++ = ThisA; // Set Alpha
}
}

// De-select bitmap
SelectObject(hTextDC, hOldBMP);
}
}

// De-select font and destroy temp DC
SelectObject(hTextDC, hOldFont);
DeleteDC(hTextDC);

// Return DIBSection
return hMyDIB;
}
'***

If you need an example of how to call it then try something like this (inDC is the DC to render to):

'***
void TestAlphaText(HDC inDC, int inX, int inY) {
const char *DemoText = "Hello World!\0";

RECT TextArea = {0, 0, 0, 0};
HFONT TempFont = CreateFont(50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial\0");
HBITMAP MyBMP = CreateAlphaTextBitmap(DemoText, TempFont, 0xFF);
DeleteObject(TempFont);

if (MyBMP) { // Create temporary DC and select new Bitmap into it
HDC hTempDC = CreateCompatibleDC(inDC);
HBITMAP hOldBMP = (HBITMAP)SelectObject(hTempDC, MyBMP);

if (hOldBMP) {
BITMAP BMInf; // Get Bitmap image size
GetObject(MyBMP, sizeof(BITMAP), &BMInf);

// Fill blend function and blend new text to window
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 0x80;
bf.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(inDC, inX, inY, BMInf.bmWidth, BMInf.bmHeight,
hTempDC, 0, 0, BMInf.bmWidth, BMInf.bmHeight, bf);

// Clean up
SelectObject(hTempDC, hOldBMP);
DeleteObject(MyBMP);
DeleteDC(hTempDC);
}
}
}
'***

Also please excuse my poor C++ syntax, it's been ages since I did any work in it..
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: ED...@mvps.org
WWW: Http://EDais.mvps.org/


Todd Aspeotis

unread,
Jul 22, 2005, 2:18:03 AM7/22/05
to
Thanks again, you keep saving my ass.

LiuHuaJun

unread,
Aug 12, 2005, 12:59:01 AM8/12/05
to
Thank you for your code, but there is a problem,
when call the function for many times, it looks like the alpha-blended text
to be accumulative,
like this link talk about,
http://www.codeproject.com/vcpp/gdiplus/gdipluscontrol.asp#xx518801xx

i try update parent window myself to clear, but the screen fliker and
performance is poor.
Is there any good solution?

"Mike D Sutton" <ED...@mvps.org> 写入消息新闻:%23zr$YykjFH...@tk2msftngp13.phx.gbl...

LiuHuaJun

unread,
Aug 12, 2005, 1:57:00 AM8/12/05
to
And the code is creating a bitmap from a new DC,
but if I want to create a bitmap from a existed DC, what can I do?
I mean I want to save the DC to the bitmap.

"Mike D Sutton" <ED...@mvps.org> 写入消息新闻:%23zr$YykjFH...@tk2msftngp13.phx.gbl...

LiuHuaJun

unread,
Aug 12, 2005, 2:00:14 AM8/12/05
to
The code is creating a bitmap from a new DC, but I want to create a bitmap
from a existed DC, how can I do?

"Mike D Sutton" <ED...@mvps.org> 写入消息新闻:%23zr$YykjFH...@tk2msftngp13.phx.gbl...

0 new messages