1

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

但结果是文本被旋转并出现在左下角,如下图所示

在此处输入图像描述

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

非常感谢,

T&T集团!

4

1 回答 1

3

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

bitmapInfo.bmiHeader.biHeight = -_imgHeight;
于 2013-06-16T12:21:22.793 回答