C++如何读取内存中的BMP,请给一个完整的代码片断,下边自己写的,摸不着头脑
BITMAPFILEHEADER bmpHeader;BITMAPINFOHEADER bmpinfoheader;
bmpHeader.bfType=0x4d42;
bmpHeader.bfSize=100;
bmpHeader.bfReserved1=0;
bmpHeader.bfReserved2=0;
bmpHeader.bfOffBits=0x36;
bmpinfoheader.biSize=sizeof(BITMAPINFOHEADER);
bmpinfoheader.biWidth=512;//512X512
bmpinfoheader.biHeight=512;
bmpinfoheader.biPlanes=0x1;
bmpinfoheader.biBitCount=0x18;
bmpinfoheader.biCompression=0;
bmpinfoheader.biSizeImage=0;
bmpinfoheader.biXPelsPerMeter=0;
bmpinfoheader.biYPelsPerMeter=0;
bmpinfoheader.biClrUsed=0;
bmpinfoheader.biClrImportant=0;
std::fstream g("c:\\1.dcm",std::ios::binary|std::ios::in);
char *pchBuf = NULL;
g.seekg (0, g.end);
long length = g.tellg();
pchBuf = (char*) malloc(length);
g.seekg (0, g.beg);
g.read((char*)pchBuf,length);
char *bmpinfo = NULL;
char *pchBuf3 = NULL;
bmpinfo = (char*) malloc(sizeof(bmpHeader) + sizeof(bmpinfoheader));
pchBuf3 = (char*) malloc(512*512*3);//rows*cols*3colors
memcpy(bmpinfo,&bmpHeader,sizeof(bmpHeader));
memcpy(bmpinfo + sizeof(bmpHeader),&bmpinfoheader,sizeof(bmpinfoheader));
unsigned char cc[512*3]={0};
unsigned char bb[3]={0};
int i,j;
for(i=0;i<=512 - 1;i++)
{
for(j=2;j<=512*2;j=j+2)
{
unsigned short int aa;
memcpy((char*)&aa,pchBuf+length-j,2);
//unsigned char bb[]={(0xff-aa/0xff)/1.5,(0xff-aa/0xff)/1.5,(0xff-aa/0xff)/1.5};
unsigned char bb[]={aa/0xff,aa/0xff,aa/0xff};
memcpy((char*)&cc + sizeof(cc) - j/2*sizeof(bb),bb,sizeof(bb));
}
memcpy(pchBuf3 + i * sizeof(cc),cc,sizeof(cc));
}//现在,bmpinfo里边有bmp头部信息,pchBuf3里有bmp的像素数据
HANDLE h;
HDC hdc,dc;
BITMAP bmp;
//下边乱写的,如何把内存总的BMP显示在屏幕上
hdc=GetDC(hwnd);//得到窗口的DC(hWnd是窗口句柄)
dc=CreateCompatibleDC(hdc);//得到与窗口DC兼容的DC
//GetDIBits(dc,0,0,512,pchBuf3,(BITMAPINFO*)bmpinfo,DIB_RGB_COLORS);
//SetDIBits(hdc,0,0,512,pchBuf3,(BITMAPINFO*)bmpinfo,DIB_RGB_COLORS);
SelectObject(dc,h);//把得到的DC与图片句柄关联起来
BitBlt(hdc,0,0,512,512,dc,0,0,SRCCOPY);//把图片画在窗体上
//DeleteDC(dc);//删除CreateCompatibleDC得到的图片DC
//ReleaseDC(hwnd,hdc);//释放GetDC得到的DC
//DeleteObject(h);//删除内存中的位图