屏幕截图
实现屏幕截图,并保存多张图片
截图的效果
void CMFCApplication1Dlg::OnBnClickedButton2()
{
CRect rect;
int left = 0;
int top = 0;
std::string cname = "a" + std::to_string(m_nScreenshotNumber) + ".bmp";
m_nScreenshotNumber++;
char name[100];
strcpy(name, cname.c_str());
GetClientRect(&rect);
left = rect.left;
top = rect.top;
CBitmap* m_pBitmap;
CFrameWnd * pMainFrame = (CFrameWnd*)AfxGetMainWnd();
CPaintDC dc(pMainFrame);
m_pBitmap = new CBitmap;
m_pBitmap->CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap memBitmap, * oldmemBitmap;
memBitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
oldmemBitmap = memDC.SelectObject(&memBitmap);
memDC.BitBlt(0, 0, rect.Width(), rect.Height(), &dc, left, top, SRCCOPY);
BITMAP bmp;
memBitmap.GetBitmap(&bmp);
FILE * fp = fopen(name, "w+b");
BITMAPINFOHEADER bih = { 0 };
bih.biBitCount = bmp.bmBitsPixel;
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
bih.biWidth = bmp.bmWidth;
BITMAPFILEHEADER bfh = { 0 };
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;
bfh.bfType = (WORD)0x4d42;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);
byte* p = new byte[bmp.bmWidthBytes * bmp.bmHeight];
GetDIBits(memDC.m_hDC, (HBITMAP)memBitmap.m_hObject, 0, rect.Height(), p,
(LPBITMAPINFO)&bih, DIB_RGB_COLORS);
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);
delete[] p;
fclose(fp);
memDC.SelectObject(oldmemBitmap);
memDC.DeleteDC();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)