我想在 Bitmap 上绘制文本,我用下面的摘要代码做到了
BITMAPINFO bitmapInfo;
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = _imgWidth;
bitmapInfo.bmiHeader.biHeight = _imgHeight;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 24;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = 0;
HDC hdc = GetDC(NULL);
if (hdc == NULL)
return false;
HFONT hFont = CreateFont( 50, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial" );
if(hFont == NULL)
return false;
HBITMAP hBitmap = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER) &bitmapInfo.bmiHeader, CBM_INIT, _BRG24arrayIn, (LPBITMAPINFO) &bitmapInfo, DIB_RGB_COLORS);
if(hBitmap == NULL)
return false;
HDC hMemDC = CreateCompatibleDC(hdc);
if (hMemDC == NULL)
return false;
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemDC, hBitmap);
if( hBitmapOld == NULL )
return false;
HFONT hFontOld = (HFONT)SelectObject(hMemDC, hFont);
if ( hFontOld == NULL )
return false;
SetBkMode(hMemDC, TRANSPARENT);
SetTextColor(hMemDC, 0x0000FF00);
RECT rect;
SetRect(&rect, 0, 0, _imgWidth, _imgHeight);
if (DrawText(hMemDC, "11:41:33", -1, &rect, DT_TOP|DT_LEFT) == 0)
return false;
GetDIBits(hdc, hBitmap, 0, _imgHeight, _BRG24arrayOut, (LPBITMAPINFO)&bitmapInfo, DIB_RGB_COLORS);
return true;
我要绘制的文本是"11:41:33"
,文本对齐是DT_TOP|DT_LEFT
但结果是文本被旋转并出现在左下角,如下图所示
输入数组_BRG24arrayIn
是BRG24
格式的,有人可以告诉我发生了什么吗?
非常感谢,
T&T集团!