As in C #, a screenshot of the entire desktop is taken.

And then how do you cut out the necessary pieces of the coordinates x1,y1,x2,y2 from this screenshot?

At the moment, so far, but I want to make 1 screen, and from it already cut the pieces.

 private Bitmap VirezPicture(int x,int y,int width,int height) // Вырезаем картинку (1) { Rectangle rect = new Rectangle(x, y, width, height); Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); return bmp; } 

UPD: Tupanul xD - You can cut the picture and so on and then cut out anything from the 1st one.

    1 answer 1

    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.

    • wow so much, try to understand ... I would like something smaller. I added an example like mine, but somewhere I dragged pieces from different places, I do not stick in it myself, but it works. - Vipz
    • @Vipz make a copy-peist and shove it into a function - and voila, you can do what you need in one line. Much less then :) - tym32167
    • crooped is x, y, width, height? If so, how to implement x1, y1, x2, y2? - Vipz
    • one
      x1,y1,x2,y2 can be easily converted to x,y,width,height - tym32167