高手们帮我看一下下面这段代码怎么执行不起来!

时间:2021-10-07 09:37:03
以下程序实现目的:
    我自己建一个8位的BMP图像,随便写几个字,再保存到硬盘上。
    保存的代码已省略,关键是我的字没有写上去(TextOut没有效果)。

HDC tempDC;
HBITMAP tempBitmap;
unsigned char *bmpbits=new unsigned char [BitmapWidth * BitmapHeight];
memset(bmpbits,0xFF,BitmapWidth*BitmapHeight);

tempDC=CreateCompatibleDC(NULL);
tempBitmap=CreateBitmap(BitmapWidth ,BitmapHeight,1,8,bmpbits);

SelectObject(tempDC,tempBitmap);

SetBkMode(tempDC,TRANSPARENT);
SetTextColor(tempDC,RGB(0xFF,0,0xFF));

TextOut(tempDC,0,0,"吴吴吴",6);

RGBQUAD *pColors=new RGBQUAD[256];
GetDIBColorTable(tempDC, 0, 256, pColors); 

unsigned char *pRgb = new unsigned char [BitmapWidth * BitmapHeight];
GetBitmapBits(tempBitmap,BitmapWidth * BitmapHeight,pRgb); //这儿取出来的数据不对,还是255,我写了几个字(TextOut(tempDC,0,0,"吴吴吴",6);)就不应该是255了吧,最后结果是我的TextOut函数无效。

9 个解决方案

#1


我把代码转换到DELPHI中去执行:
GetBitmapBits(tempBitmap,BitmapWidth * BitmapHeight,pRgb); 
pRgb取出的数据就是对的,不会全是0xFF。
照理说DELPHI也是调用的是API吧。

unsigned char 换成了Byte而已了

#2


丁页

#3


HDC hDC = ::GetDC(0);
PBYTE pbData = new BYTE[128*128*3];
ZeroMemory(pbData,128*128*3);
BITMAPINFOHEADER fmtFrame = {sizeof(BITMAPINFOHEADER), 0, 0, 1, 24, BI_RGB, 0, 0, 0, 0, 0};
fmtFrame.biWidth = 128;
fmtFrame.biHeight = 128;
fmtFrame.biSizeImage =128*128*3;
void* pBits;
HBITMAP hBmp = CreateDIBSection(hDC, (BITMAPINFO*) &fmtFrame, DIB_RGB_COLORS, (void**)&pBits, NULL, 0);
SetDIBits(hDC, hBmp, 0, 128, pbData, (BITMAPINFO*) &fmtFrame, DIB_RGB_COLORS);

CDC dc;
dc.CreateCompatibleDC(NULL);
CBitmap * pSaveBmp = dc.SelectObject(CBitmap::FromHandle(hBmp));
CFont font;
font.CreateFont(14,0,0,0,FW_THIN,0,0,0,DEFAULT_CHARSET,0,0,0,0,"Thamo"); //自定义字体
CFont * pSaveFont = dc.SelectObject(&font);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(255,0,0)); // 定义文字颜色
CSize size=dc.GetTextExtent("Hello");
int left=64-size.cx/2;
int top=64-size.cy/2;
dc.TextOut(left,top,"Hello");
dc.SelectObject(pSaveFont);
dc.SelectObject(pSaveBmp);

#4


你需要将DDB先转为DIB.

#5


http://www.codeguru.com/Cpp/G-M/bitmap/article.php/c1765/
Converting DDB to DIB

#6


http://search.csdn.net/Expert/topic/2606/2606608.xml?temp=.7435877

#7


学习ing

#8


我把代码改成下面这样就可以了

主要是调用了CreateCompatibleBitmap来产生bitmap,但是这样就不能指定颜色数,并且无法使用自己的点的buffer

HDC tempDC;
HBITMAP tempBitmap;
int BitmapWidth  = 100;
int BitmapHeight = 100;
unsigned char *bmpbits=new unsigned char [BitmapWidth * BitmapHeight];
memset(bmpbits,0xFF,BitmapWidth*BitmapHeight);

tempDC=CreateCompatibleDC(NULL);
tempBitmap=::CreateCompatibleBitmap(tempDC, 100, 100); 
::SetBitmapBits(tempBitmap, 0xFF, bmpbits);
//tempBitmap=CreateBitmap(BitmapWidth ,BitmapHeight,1,8,bmpbits);

SelectObject(tempDC,tempBitmap);

SetBkMode(tempDC,TRANSPARENT);
SetTextColor(tempDC,RGB(0xFF,0,0xFF));

TextOut(tempDC,0,0,"吴吴吴",6);
HBRUSH hbr = ::CreateSolidBrush(RGB(0, 0, 0)); 
FillRect(tempDC, CRect(0, 0, 100, 100), hbr);
RGBQUAD *pColors=new RGBQUAD[256];
GetDIBColorTable(tempDC, 0, 256, pColors); 

unsigned char *pRgb = new unsigned char [BitmapWidth * BitmapHeight];
GetBitmapBits(tempBitmap,BitmapWidth * BitmapHeight,pRgb);

#9


我前几天也遇到过这个问题,经过查找资料已经解决了,我是把矢量图融合到位图的问题,考虑你的问题和我是一样的,都是在位图上绘制文字或图形等,然后把绘制的信息保存到位图。下面这个链接可以解决你的问题。
http://blog.csdn.net/chwk/archive/2004/09/09/chwk.aspx

#1


我把代码转换到DELPHI中去执行:
GetBitmapBits(tempBitmap,BitmapWidth * BitmapHeight,pRgb); 
pRgb取出的数据就是对的,不会全是0xFF。
照理说DELPHI也是调用的是API吧。

unsigned char 换成了Byte而已了

#2


丁页

#3


HDC hDC = ::GetDC(0);
PBYTE pbData = new BYTE[128*128*3];
ZeroMemory(pbData,128*128*3);
BITMAPINFOHEADER fmtFrame = {sizeof(BITMAPINFOHEADER), 0, 0, 1, 24, BI_RGB, 0, 0, 0, 0, 0};
fmtFrame.biWidth = 128;
fmtFrame.biHeight = 128;
fmtFrame.biSizeImage =128*128*3;
void* pBits;
HBITMAP hBmp = CreateDIBSection(hDC, (BITMAPINFO*) &fmtFrame, DIB_RGB_COLORS, (void**)&pBits, NULL, 0);
SetDIBits(hDC, hBmp, 0, 128, pbData, (BITMAPINFO*) &fmtFrame, DIB_RGB_COLORS);

CDC dc;
dc.CreateCompatibleDC(NULL);
CBitmap * pSaveBmp = dc.SelectObject(CBitmap::FromHandle(hBmp));
CFont font;
font.CreateFont(14,0,0,0,FW_THIN,0,0,0,DEFAULT_CHARSET,0,0,0,0,"Thamo"); //自定义字体
CFont * pSaveFont = dc.SelectObject(&font);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(255,0,0)); // 定义文字颜色
CSize size=dc.GetTextExtent("Hello");
int left=64-size.cx/2;
int top=64-size.cy/2;
dc.TextOut(left,top,"Hello");
dc.SelectObject(pSaveFont);
dc.SelectObject(pSaveBmp);

#4


你需要将DDB先转为DIB.

#5


http://www.codeguru.com/Cpp/G-M/bitmap/article.php/c1765/
Converting DDB to DIB

#6


http://search.csdn.net/Expert/topic/2606/2606608.xml?temp=.7435877

#7


学习ing

#8


我把代码改成下面这样就可以了

主要是调用了CreateCompatibleBitmap来产生bitmap,但是这样就不能指定颜色数,并且无法使用自己的点的buffer

HDC tempDC;
HBITMAP tempBitmap;
int BitmapWidth  = 100;
int BitmapHeight = 100;
unsigned char *bmpbits=new unsigned char [BitmapWidth * BitmapHeight];
memset(bmpbits,0xFF,BitmapWidth*BitmapHeight);

tempDC=CreateCompatibleDC(NULL);
tempBitmap=::CreateCompatibleBitmap(tempDC, 100, 100); 
::SetBitmapBits(tempBitmap, 0xFF, bmpbits);
//tempBitmap=CreateBitmap(BitmapWidth ,BitmapHeight,1,8,bmpbits);

SelectObject(tempDC,tempBitmap);

SetBkMode(tempDC,TRANSPARENT);
SetTextColor(tempDC,RGB(0xFF,0,0xFF));

TextOut(tempDC,0,0,"吴吴吴",6);
HBRUSH hbr = ::CreateSolidBrush(RGB(0, 0, 0)); 
FillRect(tempDC, CRect(0, 0, 100, 100), hbr);
RGBQUAD *pColors=new RGBQUAD[256];
GetDIBColorTable(tempDC, 0, 256, pColors); 

unsigned char *pRgb = new unsigned char [BitmapWidth * BitmapHeight];
GetBitmapBits(tempBitmap,BitmapWidth * BitmapHeight,pRgb);

#9


我前几天也遇到过这个问题,经过查找资料已经解决了,我是把矢量图融合到位图的问题,考虑你的问题和我是一样的,都是在位图上绘制文字或图形等,然后把绘制的信息保存到位图。下面这个链接可以解决你的问题。
http://blog.csdn.net/chwk/archive/2004/09/09/chwk.aspx