Ejemplo n.º 1
0
        private Bitmap CaptureRectangleNative(IntPtr handle, Rectangle rect, bool captureCursor = false)
        {
            if (rect.Width == 0 || rect.Height == 0)
            {
                return(null);
            }

            if (UseWinRTCaptureAPI && ModernCaptureSignletonManager.Instance.IsAvailable)
            {
                return(CaptureRectangleDirect3D11(handle, rect, captureCursor));
            }
            else
            {
                IntPtr hdcSrc  = NativeMethods.GetWindowDC(handle);
                IntPtr hdcDest = NativeMethods.CreateCompatibleDC(hdcSrc);
                IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hdcSrc, rect.Width, rect.Height);
                IntPtr hOld    = NativeMethods.SelectObject(hdcDest, hBitmap);
                NativeMethods.BitBlt(hdcDest, 0, 0, rect.Width, rect.Height, hdcSrc, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

                if (captureCursor)
                {
                    try
                    {
                        CursorData cursorData = new CursorData();
                        cursorData.DrawCursor(hdcDest, rect.Location);
                    }
                    catch (Exception e)
                    {
                        DebugHelper.WriteException(e, "Cursor capture failed.");
                    }
                }

                NativeMethods.SelectObject(hdcDest, hOld);
                NativeMethods.DeleteDC(hdcDest);
                NativeMethods.ReleaseDC(handle, hdcSrc);
                Bitmap bmp = Image.FromHbitmap(hBitmap);
                NativeMethods.DeleteObject(hBitmap);

                return(bmp);
            }
        }
Ejemplo n.º 2
0
        private Image CaptureRectangleNative(IntPtr handle, Rectangle rect, bool captureCursor = false)
        {
            if (rect.Width == 0 || rect.Height == 0)
            {
                return(null);
            }

            IntPtr hdcSrc  = NativeMethods.GetWindowDC(handle);
            IntPtr hdcDest = NativeMethods.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hdcSrc, rect.Width, rect.Height);
            IntPtr hOld    = NativeMethods.SelectObject(hdcDest, hBitmap);

            NativeMethods.BitBlt(hdcDest, 0, 0, rect.Width, rect.Height, hdcSrc, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

            if (captureCursor)
            {
                Point cursorOffset = CaptureHelpers.ScreenToClient(rect.Location);

                try
                {
                    using (CursorData cursorData = new CursorData())
                    {
                        cursorData.DrawCursor(hdcDest, cursorOffset);
                    }
                }
                catch (Exception e)
                {
                    DebugHelper.WriteException(e, "Cursor capture failed.");
                }
            }

            NativeMethods.SelectObject(hdcDest, hOld);
            NativeMethods.DeleteDC(hdcDest);
            NativeMethods.ReleaseDC(handle, hdcSrc);
            Image img = Image.FromHbitmap(hBitmap);

            NativeMethods.DeleteObject(hBitmap);

            return(img);
        }