利用mfc保存图片控件的图片,老是保存失败,是哪里出了问题
voidCMyProjectDlg::OnBnClickedSaveButton(){HWNDhwnd=GetDlgItem(IDC_DST_IMAGE)->GetSaf...
void CMyProjectDlg::OnBnClickedSaveButton()
{ HWND hwnd = GetDlgItem(IDC_DST_IMAGE)->GetSafeHwnd(); //将IDC_DST_IMAGE控件内的图片保存上
HDC hDC = ::GetDC(hwnd); //获取DC
RECT rect;
::GetWindowRect(hwnd, &rect); //获取屏幕大小
HDC hDCMem = CreateCompatibleDC(hDC); //创建兼容DC
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC,rect.right-rect.left,rect.bottom-rect.top);//创建兼容位图
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem,hBitMap); //将位图选入DC,并保存返回值
::BitBlt(hDCMem,0,0,rect.right-rect.left,rect.bottom-rect.top,hDC,0,0,SRCCOPY);//将屏幕DC的图象复制到内存DC中
CImage cimage; //需要#include <atlimage.h>
cimage.Attach(hBitMap);
CString strFilter;
strFilter ="位图文件|*.bmp|JPEG 图像文件|*.jpg|GIF 图像文件|*.gif|PNG 图像文件|*.png||";
CFileDialog dlg(FALSE,NULL,NULL,NULL,strFilter);
if (IDOK != dlg.DoModal())
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 + _T(".") + strExtension;
}
// 图像保存
HRESULT hResult = cimage.Save(strFileName);
if (FAILED(hResult))
{
MessageBox(_T("保存图像文件失败!"));
}
cimage.Detach();
SelectObject(hDCMem,hOldMap); //复原兼容DC数据
//释放
DeleteObject(hBitMap);
DeleteDC(hDCMem);
::ReleaseDC(hwnd,hDC);
}
为什么老是显示保存图片失败? 展开
{ HWND hwnd = GetDlgItem(IDC_DST_IMAGE)->GetSafeHwnd(); //将IDC_DST_IMAGE控件内的图片保存上
HDC hDC = ::GetDC(hwnd); //获取DC
RECT rect;
::GetWindowRect(hwnd, &rect); //获取屏幕大小
HDC hDCMem = CreateCompatibleDC(hDC); //创建兼容DC
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC,rect.right-rect.left,rect.bottom-rect.top);//创建兼容位图
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem,hBitMap); //将位图选入DC,并保存返回值
::BitBlt(hDCMem,0,0,rect.right-rect.left,rect.bottom-rect.top,hDC,0,0,SRCCOPY);//将屏幕DC的图象复制到内存DC中
CImage cimage; //需要#include <atlimage.h>
cimage.Attach(hBitMap);
CString strFilter;
strFilter ="位图文件|*.bmp|JPEG 图像文件|*.jpg|GIF 图像文件|*.gif|PNG 图像文件|*.png||";
CFileDialog dlg(FALSE,NULL,NULL,NULL,strFilter);
if (IDOK != dlg.DoModal())
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 + _T(".") + strExtension;
}
// 图像保存
HRESULT hResult = cimage.Save(strFileName);
if (FAILED(hResult))
{
MessageBox(_T("保存图像文件失败!"));
}
cimage.Detach();
SelectObject(hDCMem,hOldMap); //复原兼容DC数据
//释放
DeleteObject(hBitMap);
DeleteDC(hDCMem);
::ReleaseDC(hwnd,hDC);
}
为什么老是显示保存图片失败? 展开
展开全部
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC,rect.right-rect.left,rect.bottom-rect.top);//创建兼容位图
// 图像保存
HRESULT hResult = cimage.Save(strFileName);
首先,你要确保图片加载是否成功,即hBitMap是否有效.
其次: MSDN上的例子:
// Demonstrating saving various file formats
// save an image in BMP format
m_myImage.Save(_T("c:\\temp\\image1.bmp"));
// save an image in BMP format
m_myImage.Save(_T("c:\\temp\\image2"), Gdiplus::ImageFormatBMP);
// save an image in JPEG format
m_myImage.Save(_T("c:\\temp\\image3.jpg"));
// save an image in BMP format, even though jpg file extension is used
m_myImage.Save(_T("c:\\temp\\image4.jpg"), Gdiplus::ImageFormatBMP);
最好加上第二个参数, 确定图片类型.
// 图像保存
HRESULT hResult = cimage.Save(strFileName);
首先,你要确保图片加载是否成功,即hBitMap是否有效.
其次: MSDN上的例子:
// Demonstrating saving various file formats
// save an image in BMP format
m_myImage.Save(_T("c:\\temp\\image1.bmp"));
// save an image in BMP format
m_myImage.Save(_T("c:\\temp\\image2"), Gdiplus::ImageFormatBMP);
// save an image in JPEG format
m_myImage.Save(_T("c:\\temp\\image3.jpg"));
// save an image in BMP format, even though jpg file extension is used
m_myImage.Save(_T("c:\\temp\\image4.jpg"), Gdiplus::ImageFormatBMP);
最好加上第二个参数, 确定图片类型.
更多追问追答
追问
该怎么看hBitMap是否有效.?谢谢
追答
if(NULL==hBitMap)
{
MessageBox(_T("图片创建失败!"));
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询