<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[CPU-Auslastung beim Erstellen von Screenshots]]></title><description><![CDATA[<p>Hallo Leute,<br />
beim Erstellen von Screenshots entsteht leider durch mein Programm eine sehr hohe CPU-Auslastung <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Mein Programm muss allerdings sehr oft Screenshots erstellen, jedoch sollte man am Computer noch mit Freude weiterarbeiten können... Was kann ich also tun?</p>
<pre><code class="language-cpp">bool screenshot(HWND hwnd, const char *filename) {
	int     nWidth  = GetSystemMetrics(SM_CXSCREEN);
	int     nHeight = GetSystemMetrics(SM_CYSCREEN);

	HDC     hdc     = GetDC(hwnd);
	HDC     memDC   = CreateCompatibleDC(hdc);
	HBITMAP hbm     = CreateCompatibleBitmap(hdc, nWidth, nHeight);
	HBITMAP hbmOld  = (HBITMAP)SelectObject(memDC, hbm);

	BitBlt(memDC, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY);

	BITMAPINFO bmi;

	ZeroMemory(&amp;bmi, sizeof(bmi));

	bmi.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth        = nWidth;
	bmi.bmiHeader.biHeight       = nHeight;
	bmi.bmiHeader.biBitCount     = 24;
	bmi.bmiHeader.biPlanes       = 1;
	bmi.bmiHeader.biCompression  = BI_RGB;
	bmi.bmiHeader.biSizeImage    = bmi.bmiHeader.biBitCount * nWidth * nHeight / 8;

	BYTE *pbBits = new BYTE[bmi.bmiHeader.biSizeImage];

	GetDIBits( memDC, 
				 hbm,
				 0,
				 bmi.bmiHeader.biHeight,
				 pbBits,
				 &amp;bmi,
				 DIB_RGB_COLORS );

				BITMAPFILEHEADER bfh;

				bfh.bfType      = ('M' &lt;&lt; 8) + 'B';
				bfh.bfSize      = sizeof(BITMAPFILEHEADER)  +
								  bmi.bmiHeader.biSizeImage +
								  sizeof(BITMAPINFOHEADER); 
				bfh.bfReserved1 = 0;
				bfh.bfReserved2 = 0;
				bfh.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

				HANDLE hfile = CreateFile( filename,
										   GENERIC_WRITE,
										   0,
										   0,
										   OPEN_ALWAYS,
										   0,
										   0 ); 

				DWORD dwWritten;

				WriteFile(hfile,&amp;bfh,           sizeof(bfh),               &amp;dwWritten, NULL); 
				WriteFile(hfile,&amp;bmi.bmiHeader, sizeof(BITMAPINFOHEADER),  &amp;dwWritten, NULL); 
				WriteFile(hfile,pbBits,         bmi.bmiHeader.biSizeImage, &amp;dwWritten, NULL); 

				CloseHandle(hfile); 

				SelectObject(memDC, hbmOld);
				DeleteDC(memDC);
				ReleaseDC(hwnd,hdc); 
				DeleteObject(hbm);

				delete[] pbBits;
				return true;
}
</code></pre>
<p>Vielen Dank im Voraus!</p>
<p>MrScreenshot</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/194575/cpu-auslastung-beim-erstellen-von-screenshots</link><generator>RSS for Node</generator><lastBuildDate>Tue, 30 Jun 2026 03:28:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/194575.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Oct 2007 16:13:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to CPU-Auslastung beim Erstellen von Screenshots on Mon, 08 Oct 2007 16:13:25 GMT]]></title><description><![CDATA[<p>Hallo Leute,<br />
beim Erstellen von Screenshots entsteht leider durch mein Programm eine sehr hohe CPU-Auslastung <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Mein Programm muss allerdings sehr oft Screenshots erstellen, jedoch sollte man am Computer noch mit Freude weiterarbeiten können... Was kann ich also tun?</p>
<pre><code class="language-cpp">bool screenshot(HWND hwnd, const char *filename) {
	int     nWidth  = GetSystemMetrics(SM_CXSCREEN);
	int     nHeight = GetSystemMetrics(SM_CYSCREEN);

	HDC     hdc     = GetDC(hwnd);
	HDC     memDC   = CreateCompatibleDC(hdc);
	HBITMAP hbm     = CreateCompatibleBitmap(hdc, nWidth, nHeight);
	HBITMAP hbmOld  = (HBITMAP)SelectObject(memDC, hbm);

	BitBlt(memDC, 0, 0, nWidth, nHeight, hdc, 0, 0, SRCCOPY);

	BITMAPINFO bmi;

	ZeroMemory(&amp;bmi, sizeof(bmi));

	bmi.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth        = nWidth;
	bmi.bmiHeader.biHeight       = nHeight;
	bmi.bmiHeader.biBitCount     = 24;
	bmi.bmiHeader.biPlanes       = 1;
	bmi.bmiHeader.biCompression  = BI_RGB;
	bmi.bmiHeader.biSizeImage    = bmi.bmiHeader.biBitCount * nWidth * nHeight / 8;

	BYTE *pbBits = new BYTE[bmi.bmiHeader.biSizeImage];

	GetDIBits( memDC, 
				 hbm,
				 0,
				 bmi.bmiHeader.biHeight,
				 pbBits,
				 &amp;bmi,
				 DIB_RGB_COLORS );

				BITMAPFILEHEADER bfh;

				bfh.bfType      = ('M' &lt;&lt; 8) + 'B';
				bfh.bfSize      = sizeof(BITMAPFILEHEADER)  +
								  bmi.bmiHeader.biSizeImage +
								  sizeof(BITMAPINFOHEADER); 
				bfh.bfReserved1 = 0;
				bfh.bfReserved2 = 0;
				bfh.bfOffBits   = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

				HANDLE hfile = CreateFile( filename,
										   GENERIC_WRITE,
										   0,
										   0,
										   OPEN_ALWAYS,
										   0,
										   0 ); 

				DWORD dwWritten;

				WriteFile(hfile,&amp;bfh,           sizeof(bfh),               &amp;dwWritten, NULL); 
				WriteFile(hfile,&amp;bmi.bmiHeader, sizeof(BITMAPINFOHEADER),  &amp;dwWritten, NULL); 
				WriteFile(hfile,pbBits,         bmi.bmiHeader.biSizeImage, &amp;dwWritten, NULL); 

				CloseHandle(hfile); 

				SelectObject(memDC, hbmOld);
				DeleteDC(memDC);
				ReleaseDC(hwnd,hdc); 
				DeleteObject(hbm);

				delete[] pbBits;
				return true;
}
</code></pre>
<p>Vielen Dank im Voraus!</p>
<p>MrScreenshot</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1380616</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1380616</guid><dc:creator><![CDATA[MrScreenshot]]></dc:creator><pubDate>Mon, 08 Oct 2007 16:13:25 GMT</pubDate></item><item><title><![CDATA[Reply to CPU-Auslastung beim Erstellen von Screenshots on Mon, 08 Oct 2007 16:19:07 GMT]]></title><description><![CDATA[<p>**<br />
Verzeihung!</p>
<p>Das war der Code den ich zu Testzwecken ausprobiert hatte.<br />
Dies ist der richtige Code den ich zur Zeit nutze und bei dem das beschriebene Problem auftritt!<br />
**</p>
<pre><code class="language-cpp">/*
 !!! DER RICHTIGE CODE !!!
*/
bool screenshot(HWND hwnd, const char *filename) {
	RECT rt;
	HDC hdc = GetWindowDC(hwnd);
	GetWindowRect(hwnd, &amp;rt);
	int width = rt.right - rt.left;
	int height = rt.bottom - rt.top;

	HDC hdc2;
	HBITMAP aBmp;
	BITMAPINFO bi;
	HGDIOBJ OldObj;

	void *dibvalues;

	HANDLE fileHandle;

	BITMAPFILEHEADER bmfh;
	BITMAPINFOHEADER bmih;
	DWORD bytes_write;
	DWORD bytes_written;

	hdc2 = CreateCompatibleDC(hdc);

	ZeroMemory(&amp;bmih,sizeof(BITMAPINFOHEADER));

	bmih.biSize=sizeof(BITMAPINFOHEADER);
	bmih.biHeight=height;
	bmih.biWidth=width;
	bmih.biPlanes=1;
	bmih.biBitCount=24;
	bmih.biCompression = BI_RGB;
	bmih.biSizeImage = ((((bmih.biWidth * bmih.biBitCount) + 31) &amp; ~31) &gt;&gt; 3) * bmih.biHeight;
	bmih.biXPelsPerMeter = 0;
	bmih.biYPelsPerMeter = 0;
	bmih.biClrImportant = 0;

	bi.bmiHeader = bmih;

	aBmp = CreateDIBSection(hdc,&amp;bi,DIB_RGB_COLORS,(void**)&amp;dibvalues,NULL,NULL);

	if (!aBmp) {
		return false;
	}

	OldObj = SelectObject(hdc2,aBmp);

	BitBlt(hdc2,0,0,width,height,hdc,0,0,SRCCOPY);

	ZeroMemory(&amp;bmfh,sizeof(BITMAPFILEHEADER));

	bmfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	bmfh.bfSize = (3*bmih.biHeight*bmih.biWidth)+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	bmfh.bfType = 0x4d42;
	bmfh.bfReserved1 = 0;
	bmfh.bfReserved2 = 0;

	fileHandle=CreateFile(filename,GENERIC_READ | GENERIC_WRITE,(DWORD)0,NULL,

	CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);

	if (fileHandle==INVALID_HANDLE_VALUE) {
		return false;
	}

	bytes_write = sizeof(BITMAPFILEHEADER);

	if (!WriteFile(fileHandle,(void*)&amp;bmfh,bytes_write,&amp;bytes_written,NULL)) {
		return false;
	}

	bytes_write = sizeof(BITMAPINFOHEADER);

	if (!WriteFile(fileHandle,(void*)&amp;bmih,bytes_write,&amp;bytes_written,NULL)) {
		return false;
	}

	bytes_write = bmih.biSizeImage;

	if (!WriteFile(fileHandle,(void*)dibvalues,bytes_write,&amp;bytes_written,NULL)) {
		return false;
	}

	CloseHandle(fileHandle);

	DeleteObject(SelectObject(hdc2,OldObj));
	DeleteDC(hdc2);
	ReleaseDC(hwnd,hdc); 

	return true;
}
</code></pre>
<p>Nochmals Vielen Dank im Voraus!</p>
<p>MrScreenshot</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1380625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1380625</guid><dc:creator><![CDATA[MrScreenshot]]></dc:creator><pubDate>Mon, 08 Oct 2007 16:19:07 GMT</pubDate></item><item><title><![CDATA[Reply to CPU-Auslastung beim Erstellen von Screenshots on Mon, 08 Oct 2007 19:54:03 GMT]]></title><description><![CDATA[<p>kein problem, einfach dual core rechner nutzen und das programm auf dem 2ten prozessor laufen lassen, dann kannst du sogar vom &quot;Program das viele Screenshots erstellen muß&quot; zum Echtzeitcapturen des Screens übergehn und Dir dein Werkeln später als Film angucken <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüße</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1380797</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1380797</guid><dc:creator><![CDATA[MrSolution]]></dc:creator><pubDate>Mon, 08 Oct 2007 19:54:03 GMT</pubDate></item><item><title><![CDATA[Reply to CPU-Auslastung beim Erstellen von Screenshots on Mon, 08 Oct 2007 19:57:51 GMT]]></title><description><![CDATA[<p>Omg, ich hoffe du meintest das gerade nicht wirklich ernst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1380799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1380799</guid><dc:creator><![CDATA[Fake oder Echt]]></dc:creator><pubDate>Mon, 08 Oct 2007 19:57:51 GMT</pubDate></item><item><title><![CDATA[Reply to CPU-Auslastung beim Erstellen von Screenshots on Mon, 08 Oct 2007 21:13:40 GMT]]></title><description><![CDATA[<p>Man könnte z.B. CreateDIBSection() nur 1x aufrufen und die erzeugte DIB-Section immer wieder verwenden statt bei jedem Screenshot neu aufzurufen. Genauso die BITMAPFILEHEADER-Struktur etc. sofern sich an der Bildgröße nichts ändert...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1380871</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1380871</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Mon, 08 Oct 2007 21:13:40 GMT</pubDate></item></channel></rss>