如何將視圖窗口中的自繪圖,以圖片方式保存最好是.BMP格式!並且保存的圖片要剛好是客戶區範圍之內的,同時因爲我的圖形會隨時放大,所以也要不停的進行刪除內存中保存的圖片的和重新生成,請問怎麼辦??/
VC 如何把界面一區域保存爲 bmp 圖片文件?
----------------------------------------------------------------------
用bitblt從窗體的DC往新建的Dc裏面複製
然後再把新建DC裏的東西弄到位圖裏面
--------------------------------------------------------
我這裏有一個截取客戶區圖像並保存成文件的函數,可以修改函數中拷貝區域的大小後直接使用
void CTest5View::OnGrabClient()
{
// TODO: Add your command handler code here
BITMAPINFO bi;//信息頭
void *pBits=NULL;
CRect rect;//客戶區窗口
GetClientRect(&rect);//默認拷貝區域爲客戶區大小,可以自行修改rect大小
int nWidth=rect.right;
int nHeight=rect.bottom;
//填充信息頭
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(this->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';
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);
}
}
--------------------------------------------------------
也可以把你要拷貝區域的rect做爲一個參數傳到那個函數裏,可以實現你要的功能
void CMyBView::SaveToBmp()
{
BITMAPINFO bi;//信息頭
void *pBits=NULL;
CRect rect(startpoint.x, startpoint.y, temppoint.x, temppoint.y);//客戶區窗口
// GetClientRect(&rect);
int nWidth = maxpoint.x - minpoint.x;
int nHeight= maxpoint.y - minpoint.y;
//填充信息頭
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(this->m_hWnd);
HDC hActiveWndCompatibleDC=CreateCompatibleDC(hActiveDC);
HBITMAP hActiveWndCompactibleBitmap=CreateCompatibleBitmap(hActiveDC,nWidth,nHeight);
SelectObject(hActiveWndCompatibleDC,hActiveWndCompactibleBitmap);
BitBlt(hActiveWndCompatibleDC,0,0,nWidth,nHeight,hActiveDC,minpoint.x,minpoint.y,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");//目標文件名
CFileDialog dlg(FALSE, _T("bmp"), _T("Temp.bmp"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp) |*.bmp |All Files (*.*) |*.* | |"), this);
if(dlg.DoModal() == IDOK)
{
tempFileName=dlg.GetPathName();
}
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';
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);
}
}
BitBlt(hActiveWndCompatibleDC,0,0,nWidth,nHeight,hActiveDC,minpoint.x,minpoint.y,SRCCOPY);
minpoint.x,minpoint.y, 原起點