在Bitmap上使用DrawText()时会旋转文本?

时间:2013-06-16 11:40:15

标签: c++ winapi

我想在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

但结果是文本被旋转并发生在LEFT-BOTTOM上作为结果图像

enter image description here

输入数组_BRG24arrayInBRG24格式,有人可以告诉我发生了什么?

非常感谢,

T&安培; T组

1 个答案:

答案 0 :(得分:3)

您需要否定BITMAPINFOHEADER结构中的高度以获得自上而下的位图(即第0行位于顶部而不是底部的位图)。例如:

bitmapInfo.bmiHeader.biHeight = -_imgHeight;