使用Windows GDI 截取图片


#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int main()
{
	try
	{
		int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
		int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
		HWND hDesktopWnd = GetDesktopWindow();
		HDC hDesktopDC = GetDC(hDesktopWnd);
		HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);

		for (int i = 0; i < 10; i++) {
			clock_t begin = clock();
			HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC,
				nScreenWidth, nScreenHeight);
			SelectObject(hCaptureDC, hCaptureBitmap);
			BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight,
				hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);
			CImage image;
			image.Attach(hCaptureBitmap);
			image.Save("test.jpg");
			DeleteObject(hCaptureBitmap);
			cout << double(clock() - begin) / (clock_t)1000 << endl;
		}
		ReleaseDC(hDesktopWnd, hDesktopDC);
		DeleteDC(hCaptureDC);
		IDirect3DSurface9 *surface;
		system("pause");
	}
	catch (exception& e)
	{
		cout << "\nexception thrown!" << endl;
		cout << e.what() << endl;
		system("pause");
	}
}

你可能感兴趣的:(cpp,windows,c++,算法)