MFC保存图片
添加事件处理程序于view类中
代码如下:
void CVoronoi2View::OnFileSaveAs()
{
CClientDC dc(this);
CRect rect;
//GetClientRect(&rect); //获取画布大小
rect.left = 0;
rect.right = boxlength;//boxlength为宏定义
rect.top = 0;
rect.bottom = boxwidth;
HBITMAP hbitmap = CreateCompatibleBitmap(dc, rect.right - rect.left, rect.bottom - rect.top);
//创建位图
HDC hdc = CreateCompatibleDC(dc); //创建DC,以便将图像保存为不同的格式
HBITMAP hOldMap = (HBITMAP)SelectObject(hdc, hbitmap);
//将位图选入DC,并保存返回值
BitBlt(hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top, dc, 0, 0, SRCCOPY);
//将屏幕DC的图像复制到内存DC中
CImage image;
image.Attach(hbitmap); //将位图转化为一般图像
CString saveFilePath;
{
CString strFilter = _T("位图文件(*.bmp)|*.bmp|JPEG 图像文件|*.jpg|GIF图像文件 |*.gif| PNG图像文件 |*.png|其他格式(*.*)| *.* ||");
CFileDialog dlg(FALSE, _T("bmp"), _T("Picture01.bmp"), NULL, strFilter);
if (dlg.DoModal() != IDOK)
return;
CString strFileName; //文件扩展名
CString strExtension;
strFileName = dlg.m_ofn.lpstrFile;
if (dlg.m_ofn.nFileExtension = 0)
{
switch (dlg.m_ofn.nFilterIndex)
{
case 1:
strExtension = "bmp"; break;
case 2:
strExtension = "jpg"; break;
case 3:
strExtension = "gif"; break;
case 4:
strExtension = "png"; break;
default:
break;
}
strFileName = strFileName + "." + strExtension;
}
saveFilePath = strFileName;
}
HRESULT hResult = image.Save(saveFilePath); //保存图像
image.Detach();
SelectObject(hdc, hOldMap);
}
运行之后出现
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)