包括两个函数,分别完成截图和保存的功能。
CString CFeatureComputingToolView::saveScreenshotToBitmap(CAdvertisement* pAd)
{
CRect winRect;
GetWindowRect(&winRect);
LONG left,top,right,bottom;
LONG width, height;
left = winRect.left;
top = winRect.top;
right = winRect.right;
bottom = winRect.bottom;
width = right - left;
height = bottom - top;
LONG viewUpperLeftCornerX = left+3; //border is 3 piexls width
LONG viewUpperLeftCornerY = top+3;
LONG viewWidth = width - 6;
LONG viewHeight = height - 6;
IHTMLRect *pRect;
pAd->m_pElem2->getBoundingClientRect(&pRect);
LONG adUpperLeftCornerX,adUpperLeftCornerY;
pRect->get_left(&adUpperLeftCornerX);
pRect->get_top(&adUpperLeftCornerY);
HDC hScrDC, hScreenMemDC;
HBITMAP hScreenBitmap,hOldBitmap;
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
hScreenMemDC = CreateCompatibleDC(hScrDC);
hScreenBitmap=CreateCompatibleBitmap(hScrDC,viewWidth,viewHeight);
//draw the screen shot
CString fileName;
hOldBitmap=(HBITMAP)SelectObject(hScreenMemDC,hScreenBitmap);
BitBlt(hScreenMemDC,0,0, viewWidth,viewHeight,hScrDC,viewUpperLeftCornerX,viewUpperLeftCornerY,SRCCOPY);
hScreenBitmap=(HBITMAP)SelectObject(hScreenMemDC,hOldBitmap);
// CTime curTime = CTime::GetCurrentTime();
// fileName.Format(_T("Data/Screenshots/%d-%d-%d-%d-%d-%d-%d_%d_%d.bmp"),curTime.GetYear(),curTime.GetMonth(),curTime.GetDay(),curTime.GetHour(),curTime.GetMinute(),curTime.GetSecond(),curTime.GetTickCount(),adUpperLeftCornerX,adUpperLeftCornerY);
//the file name is saved as: left-top-right-bottom_adType-adClass-adIndex.bmp
int begin = pAd->m_adID.ReverseFind('//')+1;
int end = pAd->m_adID.ReverseFind('.')-1;
CString adIndex = pAd->m_adID.Mid(begin,end-begin+1);
fileName.Format(_T("Data/Screenshots/%d-%d-%d-%d & %s-%s-%s.bmp"),adUpperLeftCornerX,adUpperLeftCornerY,adUpperLeftCornerX+pAd->m_adWidth,adUpperLeftCornerY+pAd->m_adHeight,pAd->m_adType,pAd->m_adClass,adIndex);
saveBitmapToFile(hScreenBitmap,fileName);
DeleteDC(hScrDC);
DeleteDC(hScreenMemDC);
return fileName;//return screen shot file name
}
void CFeatureComputingToolView::saveBitmapToFile(HBITMAP hBitmap, CString fileName)
{
HDC hDC;
int iBits;
WORD wBitCount;
DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;
BITMAP Bitmap;
BITMAPFILEHEADER bmfHdr;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
HANDLE fh, hDib, hPal;
HPALETTE hOldPal=NULL;
hDC = CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = 32;
if (wBitCount <= 8)
dwPaletteSize=(1<<wBitCount)*sizeof(RGBQUAD);
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight;
hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal=SelectPalette(hDC,(HPALETTE)hPal,FALSE);
RealizePalette(hDC);
}
GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize, (BITMAPINFO *)lpbi,DIB_RGB_COLORS);
if (hOldPal)
{
SelectPalette(hDC, hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
fh=CreateFile(fileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh==INVALID_HANDLE_VALUE)
return;
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL);
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
}