C++ 如何使用winapi将文本绘制到字节数组
我需要将文本绘制到字节数组,以便稍后将其转换为DirectX11纹理。 我尝试了很多想法,比如:C++ 如何使用winapi将文本绘制到字节数组,c++,winapi,C++,Winapi,我需要将文本绘制到字节数组,以便稍后将其转换为DirectX11纹理。 我尝试了很多想法,比如: HDC hdc= GetDC( g_hWnd ); int w= 600; int h= 450; unsigned* buf= new unsigned [w*h]; for( int a=0;a<w*h;a++)buf[a]= 0x0; HBITMAP hbmp= CreateBitmap( w, h, 1, 4*8, buf ); if(!hbmp)throw "erro
HDC hdc= GetDC( g_hWnd );
int w= 600;
int h= 450;
unsigned* buf= new unsigned [w*h];
for( int a=0;a<w*h;a++)buf[a]= 0x0;
HBITMAP hbmp= CreateBitmap( w, h, 1, 4*8, buf );
if(!hbmp)throw "error bmp";
HDC vhdc= CreateCompatibleDC( hdc );
if(!vhdc)throw "error vhdc";
SelectObject( vhdc, hbmp );
TextOut( vhdc, 0, 0, L"TEST", 4 );
HDC-HDC=GetDC(g_-hWnd);
int w=600;
int h=450;
无符号*buf=新的无符号[w*h];
对于(int a=0;aCreateBitmap
仅使用给定的数据作为输入。缓冲区在绘制时不会更新。您应该改用CreateDIBSection
。
HDC hdc= GetDC( g_hWnd ); /// g_hWnd is my windows handle type HWND
int w= 1024;
int h= 768;
unsigned* buf= new unsigned [w*h];
HDC vhdc= CreateCompatibleDC( hdc ); if(!vhdc)throw "error with vhdc";
HBITMAP hbmp= CreateCompatibleBitmap( hdc, w, h );
BITMAPINFO bmpi = {{sizeof(BITMAPINFOHEADER),w,-h,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}};
SelectObject( vhdc, hbmp );
TextOut( vhdc, 10, 10, L"HELLO WORLD", 11 );
GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, BI_RGB);
在该代码之后,buf使用绘制有“HELLO WORLD”的图像存储数据。当您使用带有SelectObject的位图时,该位图必须是兼容的位图。使用CreateCompatibleBitmap而不是CreateBitmap。在文本绘制之后,您可以从位图OK中获取原始字节。在测试您的答案时,这是我的错误。它可以工作。
HDC hdc= GetDC( g_hWnd ); /// g_hWnd is my windows handle type HWND
int w= 1024;
int h= 768;
unsigned* buf= new unsigned [w*h];
HDC vhdc= CreateCompatibleDC( hdc ); if(!vhdc)throw "error with vhdc";
HBITMAP hbmp= CreateCompatibleBitmap( hdc, w, h );
BITMAPINFO bmpi = {{sizeof(BITMAPINFOHEADER),w,-h,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}};
SelectObject( vhdc, hbmp );
TextOut( vhdc, 10, 10, L"HELLO WORLD", 11 );
GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, BI_RGB);