1.在stdafx.h頭檔案中加入
#include <atlimage.h>
2.儲存圖檔
方法一:
<a></a>
HBITMAP hBitmap = NULL;
//建立位圖段
BITMAPINFO bmi;
LPBYTE pBits;
ZeroMemory(&bmi,sizeof(bmi));
//m_bmpShow為Picture Control控件變量名稱
CDC *pShowDC = m_bmpShow.GetDC();
//擷取Picture Control控件的寬度和高度
//CRect m_rcShow
m_bmpShow.GetWindowRect(&m_rcShow);
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = m_rcShow.Width();
bmi.bmiHeader.biHeight = m_rcShow.Height();
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
hBitmap = CreateDIBSection(pShowDC->m_hDC,&bmi,DIB_RGB_COLORS,(void **)&pBits,0,0);
//建立相容dc并選擇位圖段
CDC dcMem;
dcMem.CreateCompatibleDC(pShowDC);
dcMem.SelectObject(hBitmap);
dcMem.BitBlt(0,0,m_rcShow.Width(),m_rcShow.Height(),pShowDC,0,0,SRCCOPY);
m_bmpShow.ReleaseDC(pShowDC);
if( hBitmap )
{
CImage img;
img.Attach(hBitmap);
img.Save(_T("f:\\1.bmp"));
img.Save(_T("f:\\1.jpg"));
//其它檔案格式同理
DeleteObject(hBitmap);
AfxMessageBox(_T("OK!!"));
}
方法二:
CDC *pdc = m_bmpShow.GetDC();
CImage imag;
CRect rcClient;
m_bmpShow.GetWindowRect(&rcClient);
imag.Create(rcClient.Width(),rcClient.Height(),32);
::BitBlt(imag.GetDC(),0,0,rcClient.Width(),rcClient.Height(),pdc->m_hDC,0,0,SRCCOPY);
imag.Save(_T("f:\\2.bmp"));
imag.Save(_T("f:\\21.jpg"));
ReleaseDC(pdc);
imag.ReleaseDC();
AfxMessageBox(_T("OK!"));
參考:http://blog.sina.com.cn/s/blog_56e19aa70100c59k.html
http://blog.sina.com.cn/s/blog_56e19aa70100c5a0.html