I have a ready-made similar code (from where it is honestly copied).
We import the necessary functions
// P/Invoke declarations [DllImport("gdi32.dll")] static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop); [DllImport("user32.dll")] static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc); [DllImport("gdi32.dll")] static extern IntPtr DeleteDC(IntPtr hDc); [DllImport("gdi32.dll")] static extern IntPtr DeleteObject(IntPtr hDc); [DllImport("gdi32.dll")] static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [DllImport("gdi32.dll")] static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("gdi32.dll")] static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp); [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr ptr);
Removing the screen
Bitmap MakeScreenShot() { var sz = Screen.PrimaryScreen.Bounds.Size; IntPtr hDesk = GetDesktopWindow(); IntPtr hSrce = GetWindowDC(hDesk); IntPtr hDest = CreateCompatibleDC(hSrce); IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height); IntPtr hOldBmp = SelectObject(hDest, hBmp); bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Bitmap bmp = Bitmap.FromHbitmap(hBmp); SelectObject(hDest, hOldBmp); DeleteObject(hBmp); DeleteDC(hDest); ReleaseDC(hDesk, hSrce); return bmp; }
Obrezalochka
Bitmap Crop(Bitmap source, Rectangle crop) { Bitmap target = new Bitmap(crop.Width, crop.Height); using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(source, new Rectangle(0, 0, target.Width, target.Height), crop, GraphicsUnit.Pixel); } return target; }
How to use
var screenshot = MakeScreenShot(); var cropped = Crop(screenshot, new Rectangle(0, 0, 300, 300));
Do not forget that bitmaps would be good if they are no longer needed.
I draw your attention that this is the screen of the main monitor, and not the entire table. How to make a screen of the entire table - find all the monitors, find out the entire width / height of the table, scroll.