Пример #1
0
        protected Bitmap GetScreenPortionAsImage(IntPtr displayContext, int positionX, int positionY, int width,
                                                 int height)
        {
            IntPtr displayCompatibleContext = WindowInvoker.CreateCompatibleDC(displayContext);
            IntPtr displayCompatibleBitmap  =
                WindowInvoker.CreateCompatibleBitmap(displayContext, width, height);

            // Select the new bitmap for future operations on our context
            IntPtr previousBitmap = WindowInvoker.SelectObject(displayCompatibleContext, displayCompatibleBitmap);

            // Copy the colour data from our desktop (and anything covering it) to our screenshot context
            bool isScreengrabSuccessful = WindowInvoker.BitBlt(displayCompatibleContext, 0, 0, width,
                                                               height, displayContext, positionX, positionY,
                                                               CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

            Bitmap screenPortionImage = isScreengrabSuccessful ? Image.FromHbitmap(displayCompatibleBitmap) : null;

            // Restore our previous image selection
            WindowInvoker.SelectObject(displayCompatibleContext, previousBitmap);

            // Clean up our created objects so we're not hogging resources
            WindowInvoker.DeleteObject(displayCompatibleBitmap);
            WindowInvoker.DeleteDC(displayCompatibleContext);

            return(screenPortionImage);
        }
Пример #2
0
        public Bitmap GetPortionAsImage(int positionX, int positionY, int width, int height)
        {
            IntPtr desktopContext = WindowInvoker.CreateDC("DISPLAY", null, null, IntPtr.Zero);

            Bitmap screenPortionImage = GetScreenPortionAsImage(desktopContext, positionX, positionY, width, height);

            WindowInvoker.DeleteDC(desktopContext);

            return(screenPortionImage);
        }