|
CADSAMPLEApp *app = (CADSAMPLEApp *)AfxGetApp(); //生成指向应用程序类的指针
HDC hMemDC;
int width, height;
HDC hDCS;
HDC m_dcCompatible;
RECT rectS1;
HWND m_hWnd1S=::GetDlgItem(m_hWnd,IDC_STATIC1);
hDCS=::GetDC(m_hWnd1S);
::GetClientRect (m_hWnd1S, &rectS1);
m_dcCompatible=::CreateCompatibleDC(hDCS);
::SelectObject(m_dcCompatible,hBitmaps);
//指向象素缓冲
BYTE *lpBitmapBits = NULL;
//初始化位图宽高
CRect rectS;
GetDlgItem(IDC_STATIC1)->GetWindowRect(&rectS);
width=(rectS.right-rectS.left);
height=(rectS.bottom-rectS.top);
//内存DC
hMemDC = CreateCompatibleDC(m_dcCompatible);//自设过渡DC
HBITMAP hBitmap=CreateCompatibleBitmap(m_dcCompatible,width,height);
SelectObject(hMemDC,hBitmap);//确定过渡DC显示表面的大小
//初始化 BITMAPINFO
//为了在WINCE上使用 CreateDIBSection ,每个象素都存储为24位,且无压缩biCompression=0)
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 建立一个HBITMAP,以获得 图像数据指针 lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC,(BITMAPINFO*)&RGB24BitsBITMAPINFO,DIB_RGB_COLORS,(void **)&lpBitmapBits,NULL,0);
//SelectObject 将directBmp选入内存DC
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
//将屏幕DC拷贝到内存DC
BitBlt(hMemDC,0,0,rectS.Width(),rectS.Height(),m_dcCompatible,0,0,SRCCOPY);//将兼容DC中的位图画到当前内存中
//位图文件头
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*3); ///3=(24 / 8);
//写入文件
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(m_dcCompatible);
DeleteObject(directBmp);
DeleteObject(previousObject);
DeleteObject(hBitmap);
这是我在wince下的位图存储,有什么问题??
我将bmiHeader.biBitCount 改成4,直接存储就有问题了,而且读取时还是读不了~~ |
|