public override Bitmap CaptureScreenArea(int X, int Y, int Width, int Height) { IntPtr hdcSrc = GDI32.CreateDC("DISPLAY", null, null, IntPtr.Zero); IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, Width, Height); GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, Width, Height, hdcSrc, User32.ScreenOffsetX + X, User32.ScreenOffsetY + Y, 0x40000000 | 0x00CC0020); //SRCCOPY AND CAPTUREBLT Bitmap bmpret = Bitmap.FromHbitmap(hBitmap); GDI32.DeleteDC(hdcSrc); GDI32.DeleteDC(hdcDest); GDI32.DeleteObject(hBitmap); return(bmpret); }
internal Screen(IntPtr monitor, IntPtr hdc) { currentDesktopChangedCount = -1; workingArea = Rectangle.Empty; IntPtr screenDC = hdc; if (!multiMonitorSupport || monitor == (IntPtr)PRIMARY_MONITOR) { // Single monitor system // if (User32.GetSystemMetrics(80) != 0) { bounds = new Rectangle(User32.GetSystemMetrics(76), User32.GetSystemMetrics(77), User32.GetSystemMetrics(78), User32.GetSystemMetrics(79)); } else { Size size = new Size(User32.GetSystemMetrics(0), User32.GetSystemMetrics(1)); bounds = new Rectangle(0, 0, size.Width, size.Height); } //bounds = SystemInformation.VirtualScreen; primary = true; deviceName = "DISPLAY"; } else { // MultiMonitor System // We call the 'A' version of GetMonitorInfoA() because // the 'W' version just never fills out the struct properly on Win2K. // var info = new User32.MonitorInfoEx(); info.Init(); User32.GetMonitorInfo(monitor, ref info); bounds = Rectangle.FromLTRB(info.Monitor.Left, info.Monitor.Top, info.Monitor.Right, info.Monitor.Bottom); primary = ((info.Flags & MONITORINFOF_PRIMARY) != 0); /*int count = info.DeviceName.Length; * while (count > 0 && info.DeviceName[count - 1] == (char)0) { * count--; * } * * deviceName = new string(info.szDevice); * deviceName = deviceName.TrimEnd((char)0);*/ deviceName = info.DeviceName; if (hdc == IntPtr.Zero) { screenDC = GDI32.CreateDC(deviceName, null, null, new HandleRef()); } } hmonitor = monitor; this.bitDepth = GDI32.GetDeviceCaps(new HandleRef(null, screenDC).Handle, 12); this.bitDepth *= GDI32.GetDeviceCaps(new HandleRef(null, screenDC).Handle, 14); if (hdc != screenDC) { GDI32.DeleteDC(new HandleRef(null, screenDC).Handle); } }
// https://www.codeproject.com/Articles/546006/Screen-Capture-on-Multiple-Monitors //function to capture screen section public static Image CaptureScreen(int x, int y, int width, int height) { //create DC for the entire virtual screen IntPtr hdcSrc = GDI32.CreateDC("DISPLAY", null, null, IntPtr.Zero); IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); GDI32.SelectObject(hdcDest, hBitmap); // set the destination area White - a little complicated Bitmap bmp = new Bitmap(width, height); Image ii = (Image)bmp; Graphics gf = Graphics.FromImage(ii); IntPtr hdc = gf.GetHdc(); //use whiteness flag to make destination screen white GDI32.BitBlt(hdcDest, 0, 0, width, height, hdc, 0, 0, 0x00FF0062); gf.Dispose(); ii.Dispose(); bmp.Dispose(); //Now copy the areas from each screen on the destination hbitmap Screen[] screendata = Screen.AllScreens; int X, X1, Y, Y1; for (int i = 0; i < screendata.Length; i++) { if (screendata[i].Bounds.X > (x + width) || (screendata[i].Bounds.X + screendata[i].Bounds.Width) < x || screendata[i].Bounds.Y > (y + height) || (screendata[i].Bounds.Y + screendata[i].Bounds.Height) < y) // no common area { } else { // something common if (x < screendata[i].Bounds.X) { X = screendata[i].Bounds.X; } else { X = x; } if ((x + width) > (screendata[i].Bounds.X + screendata[i].Bounds.Width)) { X1 = screendata[i].Bounds.X + screendata[i].Bounds.Width; } else { X1 = x + width; } if (y < screendata[i].Bounds.Y) { Y = screendata[i].Bounds.Y; } else { Y = y; } if ((y + height) > (screendata[i].Bounds.Y + screendata[i].Bounds.Height)) { Y1 = screendata[i].Bounds.Y + screendata[i].Bounds.Height; } else { Y1 = y + height; } // Main API that does memory data transfer // SRCCOPY AND CAPTUREBLT GDI32.BitBlt(hdcDest, X - x, Y - y, X1 - X, Y1 - Y, hdcSrc, X, Y, 0x40000000 | 0x00CC0020); } } // send image to clipboard Image imf = Image.FromHbitmap(new IntPtr((int)hBitmap)); //Clipboard.SetImage(imf); GDI32.DeleteDC(hdcSrc); GDI32.DeleteDC(hdcDest); GDI32.DeleteObject(hBitmap); return(imf); }