2011年11月17日 星期四

[MFC]從螢幕擷取畫面並存成bmp檔的範例程式

這段MFC範例程式會從目前視窗擷取800*600的圖片,並存成.bmp檔
檔名為目前時間,所以需要#include <ctime>
存好的檔案會放在C:\
 
 CDC* pDC = GetDC();

 //create a memory dc
 CDC memDC;
 memDC.CreateCompatibleDC(pDC);

 //Create a memory bitmap
 CBitmap newbmp;
 newbmp.CreateCompatibleBitmap(pDC, 800, 600);

 //select the bitmap in the memory dc
 CBitmap *pOldBitmap = memDC.SelectObject(&newbmp);

 //blit from screen into memory dc
 memDC.BitBlt(0,0,800,600,pDC,0,0,SRCCOPY);

 //select old bitmap back into the memory dc
 memDC.SelectObject(pOldBitmap);

 //release the Display DC
 ReleaseDC(pDC);

 time_t timestamp;
 timestamp = time (NULL);
 CString loc;
 loc.Format(_T("C:\\%ld.bmp"), timestamp);
 LPCTSTR img_name = (LPCTSTR)loc;
 CImage img;
 img.Attach((HBITMAP)newbmp.Detach());
 img.Save(img_name,Gdiplus::ImageFormatBMP);

沒有留言:

張貼留言