I am using the following code to take screenshot of the system. but while executing, the exe is taking more process, and it raises and raises for every 2 secs, system hangs, How to delete those GDI objects after saving in the file. Help me,,,
I have found that Gdiplus::Bitmap bitmap is the thing that takes huge process memory. How to delete or release this Gdiplus::Bitmap bitmap??
<code>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];
int hours,shots;
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(membit, NULL);
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);
IStream* tmpbuf = NULL;
CreateStreamOnHGlobal(NULL, true, &tmpbuf);
bitmap.Save(tmpbuf, &clsid);
HGLOBAL hg = NULL;
HRESULT hr = GetHGlobalFromStream(tmpbuf, &hg);
ULONG uSize = GlobalSize(hg);
int len=(int)uSize;
CString strDateTime;
SYSTEMTIME datetime;
::GetLocalTime(&datetime);
strDateTime.Format(_T("%02i-%02i-%02i %d:%d:%d"),
datetime.wYear,
datetime.wMonth,
datetime.wDay,
datetime.wHour,
datetime.wMinute,
datetime.wSecond);
CString strdate;
strdate.Format(_T("%02i"),datetime.wDay);
CString strmonth;
strmonth.Format(_T("%02i"),datetime.wMonth);
CString strhr;
strhr.Format(_T("%d"),datetime.wHour);
CString strmin;
strmin.Format(_T("%d"),datetime.wMinute);
CString strsec;
strsec.Format(_T("%d"),datetime.wSecond);
char pconfig[60];
CFile configIni;
configIni.Open(_T("C:\\Program Files\\MSWorks\\config.ini"),CFile::modeRead );
configIni.SeekToBegin();
configIni.Read(pconfig,sizeof(pconfig));
CString strini;
strini=pconfig;
strini.TrimRight();
CString strsub,strnew;
int nCount = 0;
for(int i=0;AfxExtractSubString(strsub,strini,i,'#');i++)
{
if(i==nCount) break;
nCount++;
}
nCount--;
CString ipaddr;
ipaddr=strip;
sprintf_s(buffer,"image%u.jpeg",c);
lpfilename=buffer;
CString strdd;
strdd=strdate+_T("-")+strmonth+_T("-")+strhr+_T("-")+strmin+_T("-")+strsec+lpfilename;
CString szDirPath = strsub+_T("\\");
CString szipaddr;
szipaddr=ipaddr;
szipaddr+=_T("-")+name+_T("\\");
CString szipadd;
szipadd=szipaddr;
CString strall;
strall=szDirPath+szipaddr;
CreateDirectory(szDirPath,NULL);
SetCurrentDirectory(szDirPath);
CreateDirectory(szipaddr,NULL);
SetCurrentDirectory(szipaddr);
CString imagesave;
imagesave=strall+strdd;
bitmap.Save(imagesave, &clsid);
c++;
int iSecret, iRandom;
iSecret = rand() % 20 + 1;
iRandom=iSecret*60000;
DeleteObject(memdc);
DeleteObject(membit);
Sleep(10000);
}
GdiplusShutdown(gdiplusToken);
}</code></pre>