<?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[Screenshot ist schwarz]]></title><description><![CDATA[<p>Moin Moin!!!</p>
<p>Ich habe eine Func, die mir einen Screenshot erstellt.<br />
Leider ist das Bild, wenn ich es in einen Viewer öffne, schwarz.<br />
Desweiteren habe ich den Verdacht, dass vorher initialisierter Speicher nicht wieder freigegeben wird. Könntet ihr bitte mal einen Blick auf die Funcs werfen? Danke.</p>
<pre><code class="language-cpp">BOOL MakeScreenshot(LPWSTR lpDestination, LPWSTR lpFileName)
{
	WCHAR path[MAX_PATH + 1];
	HDC hScreenDC, hBufferDC;
	HBITMAP hBufferBitmap;
	int nWidth, nHeight;
	PBITMAPINFO pBufferBitmapInfo;

	lstrcpy(path, lpDestination);
	PathAppend(path, lpFileName);

	hScreenDC = GetDC(0);

	nWidth = GetDeviceCaps(hScreenDC, HORZRES);
	nHeight = GetDeviceCaps(hScreenDC, VERTRES);

	hBufferDC = CreateCompatibleDC(hScreenDC);

	hBufferBitmap = CreateCompatibleBitmap(hBufferDC, nWidth, nHeight);

	if (hBufferBitmap == NULL)
		return FALSE;

	if (SelectObject(hBufferDC, hBufferBitmap) == NULL)
		return FALSE;

	if (!BitBlt(hBufferDC, 0, 0, nWidth, nHeight, hScreenDC, 0, 0, SRCCOPY))
		return FALSE;

	pBufferBitmapInfo = CreateBitmapInfo(hBufferBitmap);

	CreateBitmapFile(NULL, path, pBufferBitmapInfo, hBufferBitmap, hBufferDC);
}

PBITMAPINFO CreateBitmapInfo(HBITMAP hBmp)
{
	BITMAP bmp;
	PBITMAPINFO pbmi;
	WORD cClrBits;

	if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&amp;bmp))
		return NULL;

	// Convert the color format to a count of bits.
	cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
	if (cClrBits == 1)
		cClrBits = 1;
	else if (cClrBits &lt;= 4)
		cClrBits = 4;
	else if (cClrBits &lt;= 8)
		cClrBits = 8;
	else if (cClrBits &lt;= 16)
		cClrBits = 16;
	else if (cClrBits &lt;= 24)
		cClrBits = 24;
	else
		cClrBits = 32;

	if (cClrBits != 24) 
		pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1&lt;&lt; cClrBits));
     else 
         pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));

	pbmi-&gt;bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	pbmi-&gt;bmiHeader.biWidth = bmp.bmWidth;
	pbmi-&gt;bmiHeader.biHeight = bmp.bmHeight;
	pbmi-&gt;bmiHeader.biPlanes = bmp.bmPlanes;
	pbmi-&gt;bmiHeader.biBitCount = bmp.bmBitsPixel;

	if (cClrBits &lt; 24)
		pbmi-&gt;bmiHeader.biClrUsed = (1&lt;&lt;cClrBits);

    // If the bitmap is not compressed, set the BI_RGB flag. 
    pbmi-&gt;bmiHeader.biCompression = BI_RGB; 

    pbmi-&gt;bmiHeader.biSizeImage = ((pbmi-&gt;bmiHeader.biWidth * cClrBits +31) &amp; ~31) /8
                                  * pbmi-&gt;bmiHeader.biHeight;

	pbmi-&gt;bmiHeader.biClrImportant = 0;

	return pbmi; 
}

BOOL CreateBitmapFile(HWND hWnd, LPWSTR lpFileName, PBITMAPINFO pbi, HBITMAP hBitmap, HDC hDC)
{
	HANDLE hFile;
	BITMAPFILEHEADER hdr;
	PBITMAPINFOHEADER pbih;
	LPBYTE lpBits;
	DWORD dwTotal;
	DWORD cb;
	BYTE *hp;
	DWORD dwTmp;

    pbih = (PBITMAPINFOHEADER)pbi; 
    lpBits = (LPBYTE)GlobalAlloc(GMEM_FIXED, pbih-&gt;biSizeImage);

    if (!lpBits)
		return FALSE;

    if (!GetDIBits(hDC, hBitmap, 0, (WORD)pbih-&gt;biHeight, lpBits, pbi, DIB_RGB_COLORS))
		return FALSE;

    hFile = CreateFile(lpFileName,
					   GENERIC_READ | GENERIC_WRITE,
					   0,
					   NULL,
					   CREATE_ALWAYS,
					   FILE_ATTRIBUTE_NORMAL,
					   NULL);

    if (hFile == INVALID_HANDLE_VALUE)
		return FALSE;

    hdr.bfType = 0x4d42;

	hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) + pbih-&gt;biSize + pbih-&gt;biClrUsed * sizeof(RGBQUAD) + pbih-&gt;biSizeImage);
    hdr.bfReserved1 = 0; 
    hdr.bfReserved2 = 0; 

    // Compute the offset to the array of color indices. 
    hdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + pbih-&gt;biSize + pbih-&gt;biClrUsed * sizeof (RGBQUAD);

    if (WriteFile(hFile, (LPVOID)&amp;hdr, sizeof(BITMAPFILEHEADER),(LPDWORD)&amp;dwTmp,  NULL) == FALSE)
		return FALSE;

    if (WriteFile(hFile, (LPVOID)pbih, sizeof(BITMAPINFOHEADER) + pbih-&gt;biClrUsed * sizeof (RGBQUAD), (LPDWORD)&amp;dwTmp, NULL) == FALSE)
		return FALSE;

    dwTotal = cb = pbih-&gt;biSizeImage;
    hp = lpBits;
    if (WriteFile(hFile, (LPSTR)hp, (int)cb, (LPDWORD)&amp;dwTmp, NULL) == FALSE)
		return FALSE;

	CloseHandle(hFile);

	GlobalFree((HGLOBAL)lpBits);
}
</code></pre>
<p>ByteBooster</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/205655/screenshot-ist-schwarz</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 14:06:39 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/205655.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 16 Feb 2008 13:56:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Screenshot ist schwarz on Sat, 16 Feb 2008 13:56:17 GMT]]></title><description><![CDATA[<p>Moin Moin!!!</p>
<p>Ich habe eine Func, die mir einen Screenshot erstellt.<br />
Leider ist das Bild, wenn ich es in einen Viewer öffne, schwarz.<br />
Desweiteren habe ich den Verdacht, dass vorher initialisierter Speicher nicht wieder freigegeben wird. Könntet ihr bitte mal einen Blick auf die Funcs werfen? Danke.</p>
<pre><code class="language-cpp">BOOL MakeScreenshot(LPWSTR lpDestination, LPWSTR lpFileName)
{
	WCHAR path[MAX_PATH + 1];
	HDC hScreenDC, hBufferDC;
	HBITMAP hBufferBitmap;
	int nWidth, nHeight;
	PBITMAPINFO pBufferBitmapInfo;

	lstrcpy(path, lpDestination);
	PathAppend(path, lpFileName);

	hScreenDC = GetDC(0);

	nWidth = GetDeviceCaps(hScreenDC, HORZRES);
	nHeight = GetDeviceCaps(hScreenDC, VERTRES);

	hBufferDC = CreateCompatibleDC(hScreenDC);

	hBufferBitmap = CreateCompatibleBitmap(hBufferDC, nWidth, nHeight);

	if (hBufferBitmap == NULL)
		return FALSE;

	if (SelectObject(hBufferDC, hBufferBitmap) == NULL)
		return FALSE;

	if (!BitBlt(hBufferDC, 0, 0, nWidth, nHeight, hScreenDC, 0, 0, SRCCOPY))
		return FALSE;

	pBufferBitmapInfo = CreateBitmapInfo(hBufferBitmap);

	CreateBitmapFile(NULL, path, pBufferBitmapInfo, hBufferBitmap, hBufferDC);
}

PBITMAPINFO CreateBitmapInfo(HBITMAP hBmp)
{
	BITMAP bmp;
	PBITMAPINFO pbmi;
	WORD cClrBits;

	if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&amp;bmp))
		return NULL;

	// Convert the color format to a count of bits.
	cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
	if (cClrBits == 1)
		cClrBits = 1;
	else if (cClrBits &lt;= 4)
		cClrBits = 4;
	else if (cClrBits &lt;= 8)
		cClrBits = 8;
	else if (cClrBits &lt;= 16)
		cClrBits = 16;
	else if (cClrBits &lt;= 24)
		cClrBits = 24;
	else
		cClrBits = 32;

	if (cClrBits != 24) 
		pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1&lt;&lt; cClrBits));
     else 
         pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));

	pbmi-&gt;bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	pbmi-&gt;bmiHeader.biWidth = bmp.bmWidth;
	pbmi-&gt;bmiHeader.biHeight = bmp.bmHeight;
	pbmi-&gt;bmiHeader.biPlanes = bmp.bmPlanes;
	pbmi-&gt;bmiHeader.biBitCount = bmp.bmBitsPixel;

	if (cClrBits &lt; 24)
		pbmi-&gt;bmiHeader.biClrUsed = (1&lt;&lt;cClrBits);

    // If the bitmap is not compressed, set the BI_RGB flag. 
    pbmi-&gt;bmiHeader.biCompression = BI_RGB; 

    pbmi-&gt;bmiHeader.biSizeImage = ((pbmi-&gt;bmiHeader.biWidth * cClrBits +31) &amp; ~31) /8
                                  * pbmi-&gt;bmiHeader.biHeight;

	pbmi-&gt;bmiHeader.biClrImportant = 0;

	return pbmi; 
}

BOOL CreateBitmapFile(HWND hWnd, LPWSTR lpFileName, PBITMAPINFO pbi, HBITMAP hBitmap, HDC hDC)
{
	HANDLE hFile;
	BITMAPFILEHEADER hdr;
	PBITMAPINFOHEADER pbih;
	LPBYTE lpBits;
	DWORD dwTotal;
	DWORD cb;
	BYTE *hp;
	DWORD dwTmp;

    pbih = (PBITMAPINFOHEADER)pbi; 
    lpBits = (LPBYTE)GlobalAlloc(GMEM_FIXED, pbih-&gt;biSizeImage);

    if (!lpBits)
		return FALSE;

    if (!GetDIBits(hDC, hBitmap, 0, (WORD)pbih-&gt;biHeight, lpBits, pbi, DIB_RGB_COLORS))
		return FALSE;

    hFile = CreateFile(lpFileName,
					   GENERIC_READ | GENERIC_WRITE,
					   0,
					   NULL,
					   CREATE_ALWAYS,
					   FILE_ATTRIBUTE_NORMAL,
					   NULL);

    if (hFile == INVALID_HANDLE_VALUE)
		return FALSE;

    hdr.bfType = 0x4d42;

	hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) + pbih-&gt;biSize + pbih-&gt;biClrUsed * sizeof(RGBQUAD) + pbih-&gt;biSizeImage);
    hdr.bfReserved1 = 0; 
    hdr.bfReserved2 = 0; 

    // Compute the offset to the array of color indices. 
    hdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + pbih-&gt;biSize + pbih-&gt;biClrUsed * sizeof (RGBQUAD);

    if (WriteFile(hFile, (LPVOID)&amp;hdr, sizeof(BITMAPFILEHEADER),(LPDWORD)&amp;dwTmp,  NULL) == FALSE)
		return FALSE;

    if (WriteFile(hFile, (LPVOID)pbih, sizeof(BITMAPINFOHEADER) + pbih-&gt;biClrUsed * sizeof (RGBQUAD), (LPDWORD)&amp;dwTmp, NULL) == FALSE)
		return FALSE;

    dwTotal = cb = pbih-&gt;biSizeImage;
    hp = lpBits;
    if (WriteFile(hFile, (LPSTR)hp, (int)cb, (LPDWORD)&amp;dwTmp, NULL) == FALSE)
		return FALSE;

	CloseHandle(hFile);

	GlobalFree((HGLOBAL)lpBits);
}
</code></pre>
<p>ByteBooster</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1457106</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1457106</guid><dc:creator><![CDATA[ByteBooster]]></dc:creator><pubDate>Sat, 16 Feb 2008 13:56:17 GMT</pubDate></item></channel></rss>