[DllImport("CoreDll.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("CoreDll.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("CoreDll.dll", EntryPoint = "GetWindowDC")]
public static extern IntPtr GetWindowDC(IntPtr ptr);
[DllImport("coredll.dll")]
internal static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("coredll.dll")]
internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("coredll.dll")]
internal static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("coredll", EntryPoint = "ExtEscape")]
private static extern int ExtEscape(
IntPtr hdc,
uint nEscape,
uint cbInput,
byte[] lpszInData,
int cbOutput,
IntPtr lpszOutData
);
[DllImport("CoreDLL.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("CoreDLL.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
// Window State and Position Functions.
[DllImport("CoreDLL.dll")]
private static extern IntPtr GetCapture();
[DllImport("CoreDLL.dll")]
public static extern IntPtr SetFocus(IntPtr hWnd);
[DllImport("CoreDLL.dll")]
private static extern bool SetWindowPos(IntPtr hWnd,
IntPtr hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
int uFlags);
[DllImport("CoreDLL.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("CoreDLL.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("CoreDLL.dll", EntryPoint = "SetForegroundWindow")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
[DllImport("coredll.dll", EntryPoint = "GetForegroundWindow", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("CoreDLL.dll")]
public static extern IntPtr FindWindow(string className, string WindowsName);
把上面的内容包装到Display类中,然后。
public static Bitmap SnapShot()
{
Bitmap bmp = new Bitmap(240,320);
IntPtr hwnd = Display.GetDesktopWindow();
int width = Display.GetSystemMetrics(Display.SM_CXSCREEN);
int height = Display.GetSystemMetrics(Display.SM_CYSCREEN);
IntPtr hdcsrc = Display.GetWindowDC(hwnd);
IntPtr hdcdes = Display.CreateCompatibleDC(hdcsrc);
IntPtr hbitmap = Display.CreateCompatibleBitmap(hdcsrc, width, height);
IntPtr hOld = Display.SelectObject(hdcdes, hbitmap);
Display.BitBlt(hdcdes, 0, 0, width, height, hdcsrc, 0, -26, Display.SRCCOPY);
Display.SelectObject(hdcdes, hOld);
try
{
bmp = Bitmap.FromHbitmap(hbitmap);
Display.DeleteObject(hbitmap);
Display.DeleteObject(hOld);
Display.DeleteDC(hdcdes);
Display.DeleteDC(hdcsrc);
Display.ReleaseDC(hwnd, hdcsrc);
return bmp;
}
catch (OutOfMemoryException ome)
{
Debug.Write("MEM ERROR:"+ome.ToString());
GC.Collect();
}
return bmp;
}
}
void ScreenCapture::SaveScreenToBmpFile(const char *filename)
{
HDC hScrDC, hMemDC;
int width, height,pixel_cnt;
//the pointer will save all pixel point's color value
BYTE *lpBitmapBits = NULL;
//creates a device context for the screen device
hScrDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
//get the screen point size
width = GetDeviceCaps(hScrDC, HORZRES);
height = GetDeviceCaps(hScrDC, VERTRES);
pixel_cnt = 3 * width * height;
//creates a memory device context (DC) compatible with the screen device(hScrDC)
hMemDC = CreateCompatibleDC(hScrDC);
//initialise the struct BITMAPINFO for the bimap infomation,
//in order to use the function CreateDIBSection
//on wince os, each pixel stored by 24 bits(biBitCount=24)
//and no compressing(biCompression=0)
BITMAPINFO RGB24BitsBITMAPINFO;
ZeroMemory(&RGB24BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB24BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB24BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB24BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB24BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB24BitsBITMAPINFO.bmiHeader.biBitCount = 24;
//use the function CreateDIBSection and SelectObject
//in order to get the bimap pointer : lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB24BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);
// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, width, height, hScrDC, 0, 0, SRCCOPY);
//if you only want to get the every pixel color value,
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;
//bimap file header in order to write bmp file
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4D42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + (pixel_cnt); ///3=(24 / 8)
//if the screen does not have any change from last one, just return without saving the Bmp data.
if(! isScreenChanged(lpBitmapBits))
{
DeleteObject(hMemDC);
DeleteObject(hScrDC);
DeleteObject(directBmp);
DeleteObject(previousObject);
return;
}
//write into file
FILE *mStream = NULL;
if((mStream = fopen(filename, "wb")))
{
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(RGB24BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data
fwrite(lpBitmapBits, pixel_cnt, 1, mStream);
//close file
fclose(mStream);
}
//delete
DeleteObject(hMemDC);
DeleteObject(hScrDC);
//DeleteObject(directBmp);
DeleteObject(previousObject);
return;
}