您的位置:首页 > 其它

保存客户区数据成BMP图片

2012-11-24 23:29 387 查看
最近弄的
BITMAPINFO bi;//信息头
	 void *pBits=NULL;
	 CRect rect;//客户区窗口
	 GetClientRect(m_hWnd,&rect);
	 int nWidth=rect.right;
	 int nHeight=rect.bottom;
	 int x=rect.left;
	 //填充信息头
	 ZeroMemory(&bi,sizeof(bi));
	 bi.bmiHeader.biSize=sizeof(bi.bmiHeader);
	 bi.bmiHeader.biHeight=nHeight;
	 bi.bmiHeader.biWidth=nWidth;
	 bi.bmiHeader.biPlanes=1;
	 bi.bmiHeader.biBitCount=24;
	 bi.bmiHeader.biCompression=BI_RGB;
	 bi.bmiHeader.biSizeImage=3*nWidth*nHeight;

	 //拷贝客户区至内存DC
	 HDC hActiveDC=::GetDC(m_hWnd);
	 HDC hActiveWndCompatibleDC=CreateCompatibleDC(hActiveDC);
	 HBITMAP hActiveWndCompactibleBitmap=CreateCompatibleBitmap(hActiveDC,rect.right,rect.bottom);
	 SelectObject(hActiveWndCompatibleDC,hActiveWndCompactibleBitmap);

	 BitBlt(hActiveWndCompatibleDC,0,0,rect.right,rect.bottom,hActiveDC,0,0,SRCCOPY);

	 //保存内存DC
	 HDC hBmpFileDC=CreateCompatibleDC(hActiveWndCompatibleDC);
	 HBITMAP hBmpFileBitmap=CreateDIBSection(hActiveWndCompatibleDC,&bi,DIB_RGB_COLORS,&pBits,NULL,0);
	 SelectObject(hBmpFileDC,hBmpFileBitmap);
	 BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hActiveWndCompatibleDC,0,0,SRCCOPY);

	  CString tempFileName=_T("test.bmp");//目标文件名
	  HANDLE hFile=CreateFile(tempFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	  if(hFile!=INVALID_HANDLE_VALUE)
	  {
		   DWORD dwRet=0;
		   //填充文件头
		   BITMAPFILEHEADER bmfHeader;
		   ZeroMemory(&bmfHeader,sizeof(bmfHeader));
		   bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
		   bmfHeader.bfSize=bi.bmiHeader.biSizeImage+bmfHeader.bfOffBits;
//		   bmfHeader.bfType='MB';
		   bmfHeader.bfType=19778;
		   WriteFile(hFile,&bmfHeader,sizeof(bmfHeader),&dwRet,NULL);
		   WriteFile(hFile,&bi.bmiHeader,sizeof(bi.bmiHeader),&dwRet,NULL);
		   WriteFile(hFile,pBits,bi.bmiHeader.biSizeImage,&dwRet,NULL);
		   CloseHandle(hFile);
	  }



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: