Windows 定时抓屏

本文介绍如何利用Windows平台的GDI+库实现定时抓取屏幕图像,并保存为图片文件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) 
{ 
	UINT num = 0;		//   number   of   image   encoders 
	UINT size = 0;		//   size   of   the   image   encoder   array   in   bytes 

	ImageCodecInfo* pImageCodecInfo = NULL; 

	GetImageEncodersSize(&num,&size); 
	if(size == 0) 
		return -1;     //   Failure 

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); 
	if(pImageCodecInfo == NULL) 
		return   -1;     //   Failure 

	GetImageEncoders(num, size, pImageCodecInfo); 

	for(UINT j = 0; j < num; ++j) 
	{ 
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0) 
		{ 
			*pClsid = pImageCodecInfo[j].Clsid; 
			free(pImageCodecInfo); 
			return j;     //   Success 
		}         
	} 

	free(pImageCodecInfo); 
	return   -1;     //   Failure 
} 

void GetYOU(CString& strFileName)
{
	// TODO: Add your control notification handler code here
	HWND hwnd = ::GetDesktopWindow();   
    HDC hdc = ::GetWindowDC(NULL);   
    int x = GetDeviceCaps(hdc, HORZRES);   
    int y = GetDeviceCaps(hdc, VERTRES);   
    HBITMAP hbmp = ::CreateCompatibleBitmap(hdc, x, y), hold;   
    HDC hmemdc = ::CreateCompatibleDC(hdc);   
    hold = (HBITMAP)::SelectObject(hmemdc,   hbmp);   
    BitBlt(hmemdc, 0, 0, x, y, hdc, 0, 0, SRCCOPY);   
    SelectObject(hmemdc, hold);   
    
	//const unsigned short *pszFileName=L"C:\\new.jpg";//保存路径
	int xs=GetSystemMetrics(SM_CXSCREEN);
	int ys=GetSystemMetrics(SM_CYSCREEN);
    Bitmap bit(xs, ys), bit2(hbmp, NULL);   
    Graphics g(&bit);   
    g.ScaleTransform((float)xs/x,   (float)ys/y);   
    g.DrawImage(&bit2,   0,   0);   
    
    CLSID  encoderClsid;   
	ULONG  quality;
    quality = 30;

    EncoderParameters   encoderParameters;   
    
    encoderParameters.Count   =   1;   
    encoderParameters.Parameter[0].Guid   =   EncoderQuality;   
    encoderParameters.Parameter[0].Type   =   EncoderParameterValueTypeLong;   
    encoderParameters.Parameter[0].NumberOfValues   =   1;   
    encoderParameters.Parameter[0].Value   =   &quality;   
    
    GetEncoderClsid(L"image/jpeg",&encoderClsid);   
    bit.Save(strFileName,   &encoderClsid,   &encoderParameters);   
   
    ::DeleteObject(hbmp);   
    ::DeleteObject(hmemdc);
}



// 调用

	while(1)
	{
		Sleep(10 * 1000);
		GetSystemTime(&sysTime);
		strFileName.Format(_T("E:\\Test\\Media\\%d_%d_%d_%d_%d_%d_%d"), sysTime.wYear, sysTime.wMonth, sysTime.wDay,
			sysTime.wHour + 8, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
		GetYOU(strFileName);
	}


//  这里使用了GDI+, 注意GDI+的初始化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值