|
BMP图 24位转8位的问题
我想把24位转成8位,原图就是黑白图 想转成8位的bmp
void OnScreenSave(const char *filename)
{
HDC hScrDC, hMemDC; //屏幕和内存设备描述表
int width, height;
BYTE *lpBitmapBits = NULL; //指针用来保存每像素的颜色值
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);//为屏幕创建设备描述表
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES);
hMemDC = CreateCompatibleDC(hScrDC); //为屏幕设备描述表创建兼容的内存设备描述表
//CE下支持24位,为bitmap初始化BITMAPINFO,使用CreateDIBSection
BITMAPINFO RGB24BitsBITMAPINFO;
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
//使用CreateDIBSection和SelectObject得到bitmap指针lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO, DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);// 把屏幕设备描述表拷贝到内存设备描述表中
BITMAPFILEHEADER bmBITMAPFILEHEADER;
//bimap头文件为了写bmp文件
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42;
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3);
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))
{
//写位图文件头
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
fwrite(lpBitmapBits, 3*width*height, 1, mStream);
fclose(mStream);
}
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(previousObject);
}
|
|