在Bitmap上使用DrawText()时,文本是否旋转? [英] Text is rotated when use DrawText() on Bitmap?

查看:203
本文介绍了在Bitmap上使用DrawText()时,文本是否旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Bitmap上绘制文本,我使用下面的摘要代码。

I want to draw the text on Bitmap and I did it with the summary code below

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

The text that I want to draw is "11:41:33" and text alignment is DT_TOP|DT_LEFT

但结果是文本旋转并发生在LEFT-BOTTOM上作为下面的结果图片

But the result is the text is rotated and occurred on the LEFT-BOTTOM as result image below

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

The input array _BRG24arrayIn is in BRG24 format, someone can tell me what happen?

非常感谢,

T&

推荐答案

您需要取消 BITMAPINFOHEADER 结构中的高度自顶向下位图(即,其中行0在顶部而不是底部的位图)。例如:

You need to negate the height in the BITMAPINFOHEADER structure to get a top-down bitmap (i.e. one where row 0 is at the top rather than the bottom). For example:

bitmapInfo.bmiHeader.biHeight = -_imgHeight;

这篇关于在Bitmap上使用DrawText()时,文本是否旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆