我在Windows下编写了一个位图保存程序,写好后将其移植到WinCE平台上运行出现了GetDIBits函数不被WinCE系统所支持,上网查询说是要使用其他的函数来替换该函数所实现的功能,可是我经过长时间的查找也没有找到合适的代码,请问我的这个问题该如何解决,最好能将如下问题代码改正后给我,万分感谢,分数不够可加分。
问题代码:
/*************************************************************************************************/
/* 位图文件保存函数(传入文件保存的绝对路径和文件名称)成功运行则返回TRUE */
/*************************************************************************************************/
BOOL CFile::SaveBMPFileFun(HBITMAP hBitmap, PTSTR FilePath, PTSTR FileTitle)
{
HDC hDC = ::CreateDC ((LPCWSTR)"DISPLAY",NULL,NULL,NULL) ;
int iBits = ::GetDeviceCaps (hDC, BITSPIXEL) * ::GetDeviceCaps (hDC, PLANES) ;//当前分辨率下每个像素所占字节数
::DeleteDC(hDC) ;
WORD wBitCount ; //位图中每个像素所占字节数
if (iBits <= 1)
wBitCount = 1 ;
else if (iBits <= 4)
wBitCount = 4 ;
else if (iBits <= 8)
wBitCount = 8 ;
else if (iBits <= 24)
wBitCount = 24 ;
else
wBitCount = iBits ;
DWORD dwPaletteSize=0 ; //调色板大小, 位图中像素字节大小
if (wBitCount <= 8)
dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD) ;
BITMAP bm ; //位图属性结构
::GetObject(hBitmap, sizeof(bm), (LPSTR)&bm) ;
BITMAPINFOHEADER bi ; //位图信息头结构
bi.biSize = sizeof(BITMAPINFOHEADER) ;
bi.biWidth = bm.bmWidth ;
bi.biHeight = bm.bmHeight ;
bi.biPlanes = 1 ;
bi.biBitCount = wBitCount ;
bi.biCompression = BI_RGB ; //BI_RGB表示位图没有压缩
bi.biSizeImage = 0 ;
bi.biXPelsPerMeter = 0 ;
bi.biYPelsPerMeter = 0 ;
bi.biClrUsed = 0 ;
bi.biClrImportant = 0 ;
DWORD dwBmBitsSize = ((bm.bmWidth * wBitCount+31)/32) * 4 * bm.bmHeight ;
/*为位图内容分配内存*/
HANDLE hDib = GlobalAlloc (GHND, dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER)) ;
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib) ;
*lpbi = bi ;
HANDLE hPal = GetStockObject (DEFAULT_PALETTE) ; // 处理调色板
HANDLE hOldPal = NULL ;
if (hPal)
{
hDC = GetDC (NULL) ;
hOldPal = SelectPalette (hDC, (HPALETTE)hPal, FALSE) ;
RealizePalette (hDC) ;
}
GetDIBits (hDC, hBitmap, 0, (UINT) bm.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER)
+dwPaletteSize,(BITMAPINFO*)lpbi,DIB_RGB_COLORS);// 获取该调色板下新的像素值
if (hOldPal) // 恢复调色板
{
SelectPalette (hDC, (HPALETTE)hOldPal, TRUE) ;
RealizePalette (hDC) ;
ReleaseDC (NULL, hDC) ;
}
BITMAPFILEHEADER bmfHdr ; // 位图文件头结构
bmfHdr.bfType = 0x4D42 ; // "BM" // 设置位图文件头
DWORD dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize
+ dwBmBitsSize ; // 计算位图的大小
bmfHdr.bfSize = dwDIBSize ;
bmfHdr.bfReserved1 = 0 ;
bmfHdr.bfReserved2 = 0 ;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER)
+ dwPaletteSize ;
HANDLE hFile = CreateFile (FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;//创建位图文件
DWORD dwWritten ;
WriteFile (hFile, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL) ; // 写入位图文件头
WriteFile (hFile, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL) ;// 写入位图文件其余内容
GlobalUnlock (hDib) ; //清理
GlobalFree (hDib) ;
CloseHandle (hFile) ;
SetWindowText (gMF_hWnd, FileTitle) ; // 更改窗体标题
return TRUE ;
}
void SaveDCToBMP(HDC hDC,int nStartX,int nStartY, int nWidth,int nHeight,LPCTSTR szFileName){ //定义图形色深 int iPixel = 32;
//位图信息头 LPBITMAPINFO lpbmih = new BITMAPINFO; lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); lpbmih->bmiHeader.biWidth = nWidth; lpbmih->bmiHeader.biHeight = nHeight; lpbmih->bmiHeader.biPlanes = 1; lpbmih->bmiHeader.biBitCount = iPixel; lpbmih->bmiHeader.biCompression = BI_RGB; lpbmih->bmiHeader.biSizeImage = 0; lpbmih->bmiHeader.biXPelsPerMeter = 0; lpbmih->bmiHeader.biYPelsPerMeter = 0; lpbmih->bmiHeader.biClrUsed = 0; lpbmih->bmiHeader.biClrImportant = 0;
//创建位图数据 HDC hdcMem; HBITMAP hBitMap = NULL; CBitmap *pBitMap = NULL; CDC *pMemDC = NULL; BYTE *pBits;
hdcMem = CreateCompatibleDC(hDC); hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0); pBitMap = new CBitmap; pBitMap->Attach(hBitMap); pMemDC = new CDC; pMemDC->Attach(hdcMem); pMemDC->SelectObject(pBitMap); pMemDC->BitBlt(0,0,nWidth,nHeight,CDC::FromHandle(hDC),nStartX,nStartY,SRCCOPY);
BITMAPFILEHEADER bmfh;//位图文件头 ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER)); *((char *)&bmfh.bfType) = 'B'; *(((char *)&bmfh.bfType) + 1) = 'M'; bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); bmfh.bfSize = bmfh.bfOffBits + (nWidth * nHeight) * iPixel / 8;
int iBMPBytes = nWidth * nHeight * iPixel / 8; CFile file; if(file.Open(szFileName,CFile::modeWrite | CFile::modeCreate)) { file.Write(&bmfh,sizeof(BITMAPFILEHEADER)); file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER)); file.Write(pBits,iBMPBytes); file.Close(); } DeleteObject(hBitMap); DeleteObject(hDC); pMemDC->DeleteDC(); delete pMemDC; pMemDC = NULL; delete pBitMap; pBitMap = NULL; delete lpbmih; lpbmih = NULL;}
不好意思,刚查了一下,使一下能用么?
楼上的方法我看过了有很多的类没有定义如:CBitMap、CDC等,不能用,最好能在我原有的代码中修改,如:自己重写GetDIBits函数最好
CBitmap,CDC 在WINCE中都能用,只是你开发环境没有搭建好,一楼方法可行
SaveDCToBMP()为什么在EVC4的帮助中查不到!