Hey guys,
i hope you can help me, i have a problem.
I found in the net a code for taking a screenshot. the code works well, but the quality of the outputet bmp file is not good.
I have not enough skill to find the place where i can set the quality.
So would you be so friendly and show me the place in code and set it to the best?
Thanks for helping!
Niine
inline int GetFilePointer(HANDLE FileHandle){
return SetFilePointer(FileHandle, 0, 0, FILE_CURRENT);
}
bool SaveBMPFile(char *filename, HBITMAP bitmap, HDC bitmapDC, int width, int height)
{
bool Success=0;
HDC SurfDC=NULL;
HBITMAP OffscrBmp=NULL;
HDC OffscrDC=NULL;
LPBITMAPINFO lpbi=NULL;
LPVOID lpvBits=NULL;
HANDLE BmpFile;
BITMAPFILEHEADER bmfh;
if ((OffscrBmp = CreateCompatibleBitmap(bitmapDC, width, height)) == NULL)
return 0;
if ((OffscrDC = CreateCompatibleDC(bitmapDC)) == NULL)
return 0;
HBITMAP OldBmp = (HBITMAP)SelectObject(OffscrDC, OffscrBmp);
BitBlt(OffscrDC, 0, 0, width, height, bitmapDC, 0, 0, SRCCOPY);
if ((lpbi = (LPBITMAPINFO)(new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)])) == NULL)
return 0;
ZeroMemory(&lpbi->bmiHeader, sizeof(BITMAPINFOHEADER));
lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
SelectObject(OffscrDC, OldBmp);
if (!GetDIBits(OffscrDC, OffscrBmp, 0, height, NULL, lpbi, DIB_RGB_COLORS))
return 0;
if ((lpvBits = new char[lpbi->bmiHeader.biSizeImage]) == NULL)
return 0;
if (!GetDIBits(OffscrDC, OffscrBmp, 0, height, lpvBits, lpbi, DIB_RGB_COLORS))
return 0;
if ((BmpFile = CreateFile(filename,
GENERIC_WRITE,
0, NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
return 0;
DWORD Written;
bmfh.bfType = 19778;
bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
{
CloseHandle(BmpFile);
return 0;
}
if (Written < sizeof(bmfh))
{
CloseHandle(BmpFile);
return 0;
}
if (!WriteFile(BmpFile, &lpbi->bmiHeader, sizeof(BITMAPINFOHEADER), &Written, NULL))
{
CloseHandle(BmpFile);
return 0;
}
if (Written < sizeof(BITMAPINFOHEADER))
{
CloseHandle(BmpFile);
return 0;
}
int PalEntries;
if (lpbi->bmiHeader.biCompression == BI_BITFIELDS)
PalEntries = 3;
else PalEntries = (lpbi->bmiHeader.biBitCount <= 8) ?
(int)(1 << lpbi->bmiHeader.biBitCount) : 0;
if(lpbi->bmiHeader.biClrUsed)
PalEntries = lpbi->bmiHeader.biClrUsed;
if(PalEntries){
if (!WriteFile(BmpFile, &lpbi->bmiColors, PalEntries * sizeof(RGBQUAD), &Written, NULL))
{
CloseHandle(BmpFile);
return 0;
}
if (Written < PalEntries * sizeof(RGBQUAD))
{
CloseHandle(BmpFile);
return 0;
}
}
bmfh.bfOffBits = GetFilePointer(BmpFile);
if (!WriteFile(BmpFile, lpvBits, lpbi->bmiHeader.biSizeImage, &Written, NULL))
{
CloseHandle(BmpFile);
return 0;
}
if (Written < lpbi->bmiHeader.biSizeImage)
{
CloseHandle(BmpFile);
return 0;
}
bmfh.bfSize = GetFilePointer(BmpFile);
SetFilePointer(BmpFile, 0, 0, FILE_BEGIN);
if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
{
CloseHandle(BmpFile);
return 0;
}
if (Written < sizeof(bmfh))
{
CloseHandle(BmpFile);
return 0;
}
CloseHandle(BmpFile);
return 1;
}
bool ScreenCapture(int x, int y, int width, int height, char *filename)
{
HDC hDc = CreateCompatibleDC(0);
HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);
SelectObject(hDc, hBmp);
BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);
bool ret = SaveBMPFile(filename, hBmp, hDc, width, height);
DeleteObject(hBmp);
return ret;
}