我生成了一个CBitmap对象和它的数据内存区,把数据区颜色设置好以后装入位图,然后把CBitmap对象通过CompatipleDC显示。程序运行结果是,什么也没有,空白。请高手分析一下问题在哪里。源码: BITMAP bm;
CBitmap Bitmap; Bitmap.CreateBitmap(300,300,1,24,NULL); //创建24位位图
Bitmap.GetObject(sizeof(BITMAP),&bm);
unsigned char *pData=
new unsigned char [bm.bmHeight*bm.bmWidthBytes]; for (int y=0;y<bm.bmHeight;y++)
{
for (int x=0;x<bm.bmWidth;x++)
{
pData[x*3+y*bm.bmWidthBytes] = 0;
pData[x*3+1+y*bm.bmWidthBytes] = 0;
pData[x*3+2+y*bm.bmWidthBytes] = 255; //像素红色
}
}
Bitmap.SetBitmapBits(bm.bmHeight * bm.bmWidthBytes, pData);
delete[] pData; CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap=MemDC.SelectObject(&Bitmap);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(pOldBitmap);
CBitmap Bitmap; Bitmap.CreateBitmap(300,300,1,24,NULL); //创建24位位图
Bitmap.GetObject(sizeof(BITMAP),&bm);
unsigned char *pData=
new unsigned char [bm.bmHeight*bm.bmWidthBytes]; for (int y=0;y<bm.bmHeight;y++)
{
for (int x=0;x<bm.bmWidth;x++)
{
pData[x*3+y*bm.bmWidthBytes] = 0;
pData[x*3+1+y*bm.bmWidthBytes] = 0;
pData[x*3+2+y*bm.bmWidthBytes] = 255; //像素红色
}
}
Bitmap.SetBitmapBits(bm.bmHeight * bm.bmWidthBytes, pData);
delete[] pData; CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
CBitmap *pOldBitmap=MemDC.SelectObject(&Bitmap);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(pOldBitmap);
Bitmap.CreateBitmap(300,300,1,24,NULL); //创建24位位图
与MemDC.CreateCompatibleDC(pDC);Bitmap 与MemDC不兼容
你的屏幕可能是16位色的 CreateBitmap(300,300,1,16,NULL); 才行
还是用CreateDIBSection吧,就是麻烦点
// Create a 32 bits depth bitmap and select it into the memory DC
BITMAPINFOHEADER RGB32BITSBITMAPINFO = {
sizeof(BITMAPINFOHEADER), // biSize
bm.bmWidth, // biWidth;
bm.bmHeight, // biHeight;
1, // biPlanes;
32, // biBitCount
BI_RGB, // biCompression;
0, // biSizeImage;
0, // biXPelsPerMeter;
0, // biYPelsPerMeter;
0, // biClrUsed;
0 // biClrImportant;
};
VOID * pbits32;
HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);
BITMAP bm32;
GetObject(hbm32, sizeof(bm32), &bm32);
while (bm32.bmWidthBytes % 4)
bm32.bmWidthBytes++;
// Scan each bitmap row from bottom to top (the bitmap is inverted vertically)
BYTE *p32 = (BYTE *)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
.......
试一下下面的CBitmap bmp;
CDC memdc;
bmp.CreateCompatibleBitmap(pDC,300,300);//建立兼容显示DC的位图,色深和显示DC pDC相关
memdc.CreateCompatibleDC(pDC);
memdc.SelectObject(&bmp);//位图选入CDC中,可以借助CDC直接操作位图
CBrush br(RGB(0,0,255));
CBrush *oldbr;
oldbr=memdc.SelectObject(&br);//使用红色的刷子
memdc.Rectangle(CRect(0,0,300,300));//红色背景.//显示
pDC->BitBlt(0,0,300,300,&memdc,0,0,SRCCOPY);//我在我的程序中采用双缓冲画图,就是象上面那样做了,
//只不过我的需要比例缩放,采用的是
//pDC->SetStretchBltMode(COLORONCOLOR);
//pDC->StretchDIBits(
if (hMemDC)
{
// Get bitmap size
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm); // Create a 32 bits depth bitmap and select it into the memory DC
BITMAPINFOHEADER RGB32BITSBITMAPINFO = {
sizeof(BITMAPINFOHEADER), // biSize
bm.bmWidth, // biWidth;
bm.bmHeight, // biHeight;
1, // biPlanes;
32, // biBitCount
BI_RGB, // biCompression;
0, // biSizeImage;
0, // biXPelsPerMeter;
0, // biYPelsPerMeter;
0, // biClrUsed;
0 // biClrImportant;
};
VOID * pbits32;
HBITMAP hbm32 = CreateDIBSection(hMemDC, (BITMAPINFO *)&RGB32BITSBITMAPINFO, DIB_RGB_COLORS, &pbits32, NULL, 0);
.......