1. 程式人生 > >儲存MFC中picture control 控制元件的圖片

儲存MFC中picture control 控制元件的圖片

MFC半吊子,前段時間需要用到儲存picture control控制元件的圖片,在網上找了很久都不是自己需要的,所以自己整了一個,分享給需要的人

//儲存picture control控制元件的圖片,   引數ID是picture control控制元件ID號
void CTransferDlg::OnPictureSave(UINT ID)
{
	CWnd* bmpShow = GetDlgItem(ID);
	CDC *pdc = bmpShow->GetDC();
	CImage  imag;
	CRect rect;

	GetClientRect(&rect);        //獲取畫布大小
	bmpShow->GetWindowRect(&rect);
	imag.Create(rect.Width(), rect.Height(), 32);
	::BitBlt(imag.GetDC(), 0, 0, rect.Width(), rect.Height(), pdc->m_hDC, 0, 0, SRCCOPY);
	
	TCHAR szFilter[] = _T("jpg file(*.jpg)|*.jpg|bmp file(*.bmp)|*.bmp|所有檔案(*.*)|*.*||");  //檔案格式過濾
	// 構造儲存檔案對話方塊    
	CFileDialog fileDlg(FALSE, _T("jpg"), _T("*.jpg"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
	fileDlg.m_ofn.lpstrTitle = _T("儲存直方圖");  //儲存對話視窗標題名
	CString picturePath;
	if (IDOK == fileDlg.DoModal())  //按下確認鍵
	{
		picturePath = fileDlg.GetPathName();  //檔案路徑
	}

	HRESULT hResult = imag.Save(picturePath); //儲存圖片
	ReleaseDC(pdc);
	imag.ReleaseDC();
}