public static Bitmap GetDesktop() { IntPtr dC = WindowsAPI.GetDC(WindowsAPI.GetDesktopWindow()); IntPtr intPtr = WindowsAPI.CreateCompatibleDC(dC); int systemMetrics = WindowsAPI.GetSystemMetrics(0); int systemMetrics2 = WindowsAPI.GetSystemMetrics(1); IntPtr intPtr2 = WindowsAPI.CreateCompatibleBitmap(dC, systemMetrics, systemMetrics2); Bitmap result; if (intPtr2 != IntPtr.Zero) { IntPtr hgdiobjBm = WindowsAPI.SelectObject(intPtr, intPtr2); WindowsAPI.BitBlt(intPtr, 0, 0, systemMetrics, systemMetrics2, dC, 0, 0, 13369376); WindowsAPI.SelectObject(intPtr, hgdiobjBm); WindowsAPI.DeleteDC(intPtr); WindowsAPI.ReleaseDC(WindowsAPI.GetDesktopWindow(), dC); Bitmap bitmap = Image.FromHbitmap(intPtr2); WindowsAPI.DeleteObject(intPtr2); GC.Collect(); result = bitmap; } else { result = null; } return(result); }
/// <summary> /// 截取桌面指定区域的矩形 /// </summary> /// <param name="rectangle"></param> /// <returns></returns> public static Bitmap GetDesktop(System.Drawing.Rectangle rectangle) { IntPtr dC = WindowsAPI.GetDC(WindowsAPI.GetDesktopWindow()); IntPtr intPtr = WindowsAPI.CreateCompatibleDC(dC); IntPtr intPtr2 = WindowsAPI.CreateCompatibleBitmap(dC, rectangle.Width, rectangle.Height); Bitmap result; if (intPtr2 != IntPtr.Zero) { IntPtr hgdiobjBm = WindowsAPI.SelectObject(intPtr, intPtr2); WindowsAPI.BitBlt(intPtr, 0, 0, rectangle.Width, rectangle.Height, dC, rectangle.X, rectangle.Y, 13369376); //WindowsAPI.SelectObject(intPtr, hgdiobjBm); WindowsAPI.DeleteDC(intPtr); WindowsAPI.ReleaseDC(WindowsAPI.GetDesktopWindow(), dC); Bitmap bitmap = Image.FromHbitmap(intPtr2); DeleteObject(intPtr2); result = bitmap; bitmap = null; } else { result = null; } return(result); }
/// <summary> /// 获取屏幕中鼠标所在位置的点的颜色值 /// </summary> /// <param name="ptr">获取的范围句柄设定(0为全屏幕)</param> /// <returns>返回颜色值</returns> public static Color GetMousePointColor(IntPtr ptr) { IntPtr hdc = WindowsAPI.GetDC(ptr); uint pixel = WindowsAPI.GetPixel(hdc, Cursor.Position.X, Cursor.Position.Y); WindowsAPI.ReleaseDC(IntPtr.Zero, hdc); Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16); return(color); }