Below is a code which use data in array "data " to make a 256 color graylevel bitmap and place it on the screen with the use of BitBlt.I hope it helps whoever needs such a code.Let me know weather it is helpful or not for you.
int nColors=256; RGBQUAD *pRGB; pRGB = new RGBQUAD[nColors]; for (i=0; i <256; i++) { pRGB[i].rgbRed=i; pRGB[i].rgbGreen=i; pRGB[i].rgbBlue=i; pRGB[i].rgbReserved=0; }
BYTE *datas;
int i,j,k=0;
CClient DC(AfxGetMainWnd());
BITMAPINFO *BitInfo;
BitInfo=new BITMAPINFO;
BitInfo- >bmiHeader.biSize=sizeof (BITMAPINFOHEADER);
BitInfo- >bmiHeader.biWidth=col;
BitInfo- >bmiHeader.biHeight=-row;
BitInfo- >bmiHeader.biPlanes=1;
BitInfo- >bmiHeader.biBitCount=8;
BitInfo- >bmiHeader.biCompression=0;
CDC memDC;
memDC.CreateCompatibleDC(&DC);
HBITMAP hbmp=CreateDIBSection(memDC.m_hDC,
BitInfo,
DIB_RGB_COLORS,
(void **)&datas,
NULL,
0);
HBITMAP oldBmp=(HBITMAP)SelectObject(memDC.m_hDC,hbmp);
int nColors=256;
RGBQUAD *pRGB;
pRGB = new RGBQUAD[nColors];
for (i=0; i <256; i++) {
pRGB[i].rgbRed=i;
pRGB[i].rgbGreen=i;
pRGB[i].rgbBlue=i;
pRGB[i].rgbReserved=0;
}
::SetDIBColorTable(memDC.m_hDC,0,nColors,pRGB);
for(i=0;i <row;i+=decimation)
for(j=0;j <col;j+=decimation)
datas[k++]=data[i][j]*scale;
DC.BitBlt(_point.x,_point.y,col,row,&memDC, 0,0,SRCCOPY);
DeleteObject(hbmp);
delete BitInfo;
delete pRGB;
Submitted By: Mehdi Esnaashari (2002/03/09)
你的代码的意思是不是自己构造bmp图象的文件头信息,然后把文件头信息和图象数据一起组成一个bmp文件,然后定义色表,显示图象?
我对图象处理方面不是很熟悉,有什么相关网站或则有什么好的书推荐吗?
//画像のデータを取得する。
LPBITMAPINFO lpBitmap;
lpBitmap=(BITMAPINFO*) new BYTE[sizeof(BITMAPINFOHEADER) +
GetColorNumber(8 * img->nChannels) * sizeof(RGBQUAD)];
lpBitmap->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
lpBitmap->bmiHeader.biWidth=100;
lpBitmap->bmiHeader.biHeight=100;
lpBitmap->bmiHeader.biBitCount=(WORD)(8 * 1);
lpBitmap->bmiHeader.biPlanes=1;
lpBitmap->bmiHeader.biCompression=BI_RGB;
lpBitmap->bmiHeader.biSizeImage=0;
lpBitmap->bmiHeader.biClrUsed=0;
BYTE* bits = (BYTE *) datas;
//HBITMAP画像をクリエートする。
CDC dc;
dc.CreateDC("DISPLAY", "DISPLAY", 0, 0);
hBitmap = CreateDIBitmap(dc.m_hDC,&lpBitmap->bmiHeader,CBM_INIT,bits,lpBitmap,DIB_RGB_COLORS|DIB_RGB_COLORS);
DeleteDC(dc);
//HBITMAP画像をリターンする。
return hBitmap;