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+的初始化