對屏幕截屏,保存爲BMP格式文件的實現方法(VC2003)
ScreenManage Class defines
// Get the screen's content and save to BMP file
VOID ScreenManage::GetScreen(CString strOutputFileName)
{
CDC *pDC; // Screen's DC
pDC = CDC::FromHandle(GetDC(NULL));
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL); // Get the color mode
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);
CDC memDC; // Memory DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap; // Create one bitmap for the screen
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&memBitmap); // Save to memory
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY); // Copy the screen to memory
// Saving memDC to BMP file process
BITMAP bmp;
memBitmap.GetBitmap(&bmp); // Get the bitMap's information
FILE *fp = fopen(strOutputFileName, "w+b");
BITMAPINFOHEADER bih = {0}; // bitMap's header
bih.biBitCount = bmp.bmBitsPixel; // the bitMap pixel's size
//bih.biBitCount = 32; // the bitMap pixel's size
//bih.biBitCount = 24; // the bitMap pixel's size
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight; // set Height
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight; // set the bitMap's size
bih.biWidth = bmp.bmWidth; // set Width
BITMAPFILEHEADER bfh = {0}; // bitMap file's header
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight; // bitMap file's size
bfh.bfType = (WORD)0x4D42;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp); // write the bitMap file header
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp); // write the bitMap header
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight]; //
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS); //
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//
delete [] p;
fclose(fp);
memDC.SelectObject(oldmemBitmap);
return VOID();
}
// Get screen's map by some parameters
VOID ScreenManage::GetScreen(CString strOutputFileName, int nPuaseTimer, int nTotalMapCount)
{
int nCount = 0;
CString strNewFileName;
CStringArray arrFileNameData;
TypeConver typeConver;
typeConver.SplitString(strOutputFileName,'.',arrFileNameData);
while(nCount < nTotalMapCount)
{
CString strCount;
typeConver.IntToCString(nCount, strCount);
strNewFileName = arrFileNameData.GetAt(0) + strCount + "." + arrFileNameData.GetAt(1);
this->GetScreen(strNewFileName);
Sleep(nPuaseTimer); // Puase process
nCount++;
}
}
TypeConver Class defines
//
VOID TypeConver::SplitString(CString & strInputData, TCHAR cSplitChar, CStringArray & arrOutputData)
{
TCHAR * p = strInputData.GetBuffer(0);
TCHAR * e = p;
TCHAR cEnd = * e;
int nCount = 0;
while(cEnd)
{
if(*e == _T('/0'))
cEnd = * e;
else if(*e == cSplitChar)
*e = _T('/0');
if(*e)
e++;
else
{
if(*p != _T('/0'))
{
arrOutputData.Add(p);
nCount++;
}
p = ++e;
}
}
return VOID();
}
//
VOID TypeConver::IntToCString(int nInputData, CString & strOutputData)
{
strOutputData.Format("%08d",nInputData);
return VOID();
}
//
int TypeConver::CStringToInt(CString strInputData)
{
return atoi(strInputData);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.