I have developed a screenshot application in VC++ 2005. It will take screenshots and save it in the server MYSQL database. The process is that, initially the file will be saving in C: drive, then after taking the blob data, the file will be removed. But it has been installed almost all system in my office. The problem is that, it takes more than 65% CPU usage. How to avoid this. Please help me. AFX_ODBC_CALL(::SQLExecute(m hstmt) takes too long......
void gdiscreen()
{
using namespace Gdiplus;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
int c=0;
HDC scrdc, memdc;
HBITMAP membit;
HBITMAP hOldBitmap;
CString lpfilename;
char buffer[1000];
for(;;)
{
scrdc = ::GetDC(0);
int Height = GetSystemMetrics(SM_CYSCREEN);
int Width = GetSystemMetrics(SM_CXSCREEN);
memdc = CreateCompatibleDC(scrdc);
membit = CreateCompatibleBitmap(scrdc, Width, Height);
hOldBitmap=(HBITMAP) SelectObject(memdc, membit);
BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
Gdiplus::Bitmap* bitmap = ::new Bitmap(membit, NULL );
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);
sprintf_s(buffer,"C:\\Program Files\\image%u.jpeg",c);
lpfilename=buffer;
bitmap->Save(lpfilename, &clsid);
IStream* tmpbuf = NULL;
CreateStreamOnHGlobal(NULL, true, &tmpbuf);
bitmap->Save(tmpbuf, &clsid);
HGLOBAL hg = NULL;
HRESULT hr = GetHGlobalFromStream(tmpbuf, &hg);
SIZE_T uSize = GlobalSize(hg);
int len=(int)uSize;
CFile file;
BYTE buf[150];
CStringA charstr(lpfilename);
CString strblob;
const char *szSingle;
szSingle=((const char *) charstr);
CString strDateTime;
SYSTEMTIME datetimenew;
::GetLocalTime(&datetimenew);
strDateTime.Format(_T("%02i-%02i-%02i %d:%d:%d"),
datetimenew.wYear,
datetimenew.wMonth,
datetimenew.wDay,
datetimenew.wHour,
datetimenew.wMinute,
datetimenew.wSecond);
CdbImages dbImages(&theApp.m_DB);
dbImages.Open();
dbImages.AddNew();
CFile fileImage;
CFileStatus fileStatus;
fileImage.Open(lpfilename, CFile::modeRead);
fileImage.GetStatus(fileStatus);
dbImages.m_dateTime = strDateTime;
dbImages.m_BLOBName = SqlStr3;
dbImages.m_BLOBImage.m_dwDataLength = fileStatus.m_size;
HGLOBAL hGlobal = GlobalAlloc(GPTR,fileStatus.m_size);
dbImages.m_BLOBImage.m_hData = GlobalLock(hGlobal);
fileImage.Read(dbImages.m_BLOBImage.m_hData,fileStatus.m_size);
dbImages.SetFieldDirty(&dbImages.m_BLOBImage);
dbImages.SetFieldNull(&dbImages.m_BLOBImage,FALSE);
dbImages.m_log_user = loguser;
dbImages.Update();
fileImage.Close();
dbImages.Close();
GlobalUnlock(hGlobal);
::delete bitmap;
bitmap = NULL;
DeleteObject(memdc);
DeleteObject(membit);
::ReleaseDC(0,scrdc);
CFile::Remove(lpfilename);
int iSecret, iRandom;
iSecret = rand() % 20 + 1;
iRandom=iSecret*60000;
Sleep(iRandom);
c++;
}
GdiplusShutdown(gdiplusToken);
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
using namespace Gdiplus;
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1;
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1;
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;
}
}
free(pImageCodecInfo);
return 0;
}