Ejemplo n.º 1
0
        private static BitmapSource CaptureRegion(IntPtr hWnd, int x, int y, int width, int height, bool addToClipboard)
        {
            IntPtr       sourceDC = IntPtr.Zero;
            IntPtr       targetDC = IntPtr.Zero;
            IntPtr       compatibleBitmapHandle = IntPtr.Zero;
            BitmapSource bitmap = null;

            sourceDC = User32.GetDC(User32.GetDesktopWindow());
            targetDC = Gdi32.CreateCompatibleDC(sourceDC);

            compatibleBitmapHandle = Gdi32.CreateCompatibleBitmap(sourceDC, width, height);

            Gdi32.SelectObject(targetDC, compatibleBitmapHandle);

            Gdi32.BitBlt(targetDC, 0, 0, width, height, sourceDC, x, y, Gdi32.SRCCOPY);

            bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                compatibleBitmapHandle, IntPtr.Zero, Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            if (addToClipboard)
            {
                IDataObject data = new DataObject();
                data.SetData(DataFormats.Dib, bitmap, false);
                Clipboard.SetDataObject(data, false);
            }
            Gdi32.DeleteObject(compatibleBitmapHandle);

            User32.ReleaseDC(IntPtr.Zero, sourceDC);
            User32.ReleaseDC(IntPtr.Zero, targetDC);

            return(bitmap);
        }
Ejemplo n.º 2
0
        static Bitmap CaptureRegionUnmanaged(Rectangle Region, bool IncludeCursor = false)
        {
            IntPtr hSrc    = Gdi32.CreateDC("DISPLAY", null, null, 0),
                   hDest   = Gdi32.CreateCompatibleDC(hSrc),
                   hBmp    = Gdi32.CreateCompatibleBitmap(hSrc, Region.Width, Region.Height),
                   hOldBmp = Gdi32.SelectObject(hDest, hBmp);

            Gdi32.BitBlt(hDest, 0, 0,
                         Region.Width, Region.Height,
                         hSrc,
                         Region.Left, Region.Top,
                         CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

            var bmp = Image.FromHbitmap(hBmp);

            Gdi32.SelectObject(hDest, hOldBmp);
            Gdi32.DeleteObject(hBmp);
            Gdi32.DeleteDC(hDest);
            Gdi32.DeleteDC(hSrc);

            var clone = bmp.Clone(new Rectangle(Point.Empty, bmp.Size), PixelFormat.Format24bppRgb);

            if (IncludeCursor)
            {
                new MouseCursor().Draw(clone, Region.Location);
            }

            return(clone);
        }
Ejemplo n.º 3
0
        public Bitmap CaptureWindowGDI(IntPtr handle)
        {
            var hdcSrc = User32.GetWindowDC(handle);

            var windowRect = new RECT();

            User32.GetWindowRect(handle, ref windowRect);

            var width  = windowRect.right - windowRect.left;
            var height = windowRect.bottom - windowRect.top;

            var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
            var hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

            var hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 13369376);
            Gdi32.SelectObject(hdcDest, hOld);
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            var image = Image.FromHbitmap(hBitmap);

            Gdi32.DeleteObject(hBitmap);

            return(image);
        }
Ejemplo n.º 4
0
        private void FromWindow(IntPtr hwnd)
        {
            IntPtr hDc = User32.GetDC(hwnd);

            IntPtr memhDC = Gdi32.CreateCompatibleDC(hDc);

            User32.RECT clientSize;
            User32.GetClientRect(hwnd, out clientSize);

            Point bounds = new Point(clientSize.Right - clientSize.Left, clientSize.Bottom - clientSize.Top);

            IntPtr hBmp = Gdi32.CreateCompatibleBitmap(hDc, bounds.X, bounds.Y);

            Gdi32.SelectObject(memhDC, hBmp);

            Gdi32.BitBlt(memhDC, 0, 0, bounds.X, bounds.Y, hDc, 0, 0, Gdi32.TernaryRasterOperations.SRCCOPY);

            Bitmap bmp = Bitmap.FromHbitmap(hBmp);

            Gdi32.DeleteObject(hBmp);
            User32.ReleaseDC(hwnd, hDc);
            Gdi32.DeleteDC(memhDC);

            FromBitmap(bmp);
        }
Ejemplo n.º 5
0
        private void SetSpotInfo()
        {
            int x = _originX;
            int y = _originY;

            IntPtr hwnd  = User32.GetDesktopWindow();
            IntPtr dc    = User32.GetWindowDC(hwnd);
            IntPtr memDC = Gdi32.CreateCompatibleDC(dc);
            IntPtr hbm   = Gdi32.CreateCompatibleBitmap(dc, _spotWidth, _spotHeight);
            IntPtr oldbm = Gdi32.SelectObject(memDC, hbm);

            Gdi32.BitBlt(
                memDC,
                0, 0,
                this.SpotWidth, this.SpotHeight,
                dc,
                x, y,
                0x40CC0020
                );

            _currentSpotBmp = Image.FromHbitmap(hbm);

            Gdi32.SelectObject(memDC, oldbm);
            Gdi32.DeleteObject(hbm);
            Gdi32.DeleteDC(memDC);
            User32.ReleaseDC(hwnd, dc);
        }
Ejemplo n.º 6
0
        public static void BitBlt(Graphics g, IntPtr hObject, Rectangle srcRect, Point destPoint)
        {
            IntPtr pTarget = g.GetHdc();

            try
            {
                IntPtr pSource = Gdi32.CreateCompatibleDC(pTarget);
                try
                {
                    IntPtr pOrig = Gdi32.SelectObject(pSource, hObject);
                    try
                    {
                        Gdi32.BitBlt(pTarget, destPoint.X, destPoint.Y, srcRect.Width, srcRect.Height, pSource, srcRect.X, srcRect.Y,
                                     Gdi32.TernaryRasterOperations.SRCCOPY);
                    }
                    finally
                    {
                        Gdi32.SelectObject(pSource, pOrig);
                    }
                }
                finally
                {
                    Gdi32.DeleteDC(pSource);
                }
            }
            finally
            {
                g.ReleaseHdc(pTarget);
            }
        }
Ejemplo n.º 7
0
        public Bitmap Capture()
        {
            if (GetWindowThreadProcessId() == 0)
            {
                return(new Bitmap(1, 1, PixelFormat.Format24bppRgb));
            }

            var deviceSrc = User32.GetWindowDC(this.windowHandle);

            User32.GetWindowRect(this.windowHandle, ref this.rect);
            var deviceDest   = Gdi32.CreateCompatibleDC(deviceSrc);
            var bitmapHandle = Gdi32.CreateCompatibleBitmap(
                deviceSrc,
                this.rect.Width,
                this.rect.Height);
            var oldHandle = Gdi32.SelectObject(deviceDest, bitmapHandle);

            Gdi32.BitBlt(deviceDest, 0, 0, this.rect.Width, this.rect.Height, deviceSrc, 0, 0, 0x00CC0020);
            Gdi32.SelectObject(deviceDest, oldHandle);
            Gdi32.DeleteDC(deviceDest);
            User32.ReleaseDC(this.windowHandle, deviceSrc);

            var img = Image.FromHbitmap(bitmapHandle);

            Gdi32.DeleteObject(bitmapHandle);

            return(img);
        }
Ejemplo n.º 8
0
        public static Bitmap CaptureDesktopWindow()
        {
            //创建屏幕句柄
            Graphics grpWindow = Graphics.FromHwnd(IntPtr.Zero);

            //创建一幅保存图像
            Bitmap bitmap = new Bitmap((int)grpWindow.VisibleClipBounds.Width, (int)grpWindow.VisibleClipBounds.Height, grpWindow);

            //创建bitmap相关的Grp类
            Graphics grpBitmap = Graphics.FromImage(bitmap);

            //窗口上下文
            IntPtr hdcWindow = grpWindow.GetHdc();

            //图片的上下文
            IntPtr hdcBitmap = grpBitmap.GetHdc();

            //拷贝
            Gdi32.BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdcWindow, 0, 0, 0x00CC0020);

            //释放关联grpBitmap句柄
            grpBitmap.ReleaseHdc(hdcBitmap);

            //释放关联grpWindow句柄
            grpWindow.ReleaseHdc(hdcWindow);

            //释放grpBitmap对象
            grpBitmap.Dispose();

            //释放grpWindow对象
            grpWindow.Dispose();

            //返回图像
            return(bitmap);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates screenshot of all available screens. <br/>
        /// If <paramref name="cropRectangle"/> is not null, returns image of this region.
        /// </summary>
        /// <param name="cropRectangle"></param>
        /// <returns></returns>
        public static Bitmap Shot(Rectangle?cropRectangle = null)
        {
            var rectangle = (cropRectangle ?? GetPhysicalScreenRectangle()).Normalize();

            var window = User32.GetDesktopWindow();

            using var dc   = User32.GetWindowDC(window);
            using var toDc = Gdi32.CreateCompatibleDC(dc);
            var hBmp    = Gdi32.CreateCompatibleBitmap(dc, rectangle.Width, rectangle.Height);
            var hOldBmp = Gdi32.SelectObject(toDc, hBmp);

            // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
            Gdi32.BitBlt(toDc.DangerousGetHandle(),
                         0,
                         0,
                         rectangle.Width,
                         rectangle.Height,
                         dc.DangerousGetHandle(),
                         rectangle.X,
                         rectangle.Y,
                         (int)(CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy));

            var bitmap = Image.FromHbitmap(hBmp);

            Gdi32.SelectObject(toDc, hOldBmp);
            Gdi32.DeleteObject(hBmp).Check();
            Gdi32.DeleteDC(toDc).Check();                              //?
            User32.ReleaseDC(window, dc.DangerousGetHandle()).Check(); //?

            return(bitmap);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// A képernyő egy adott régiójáról készít képernyőképet.
        /// </summary>
        /// <param name="hWnd">Az ablak Handle</param>
        /// <param name="x">x koordináta</param>
        /// <param name="y">y koordináta</param>
        /// <param name="width">A kép szélessége (A terület szélessége)</param>
        /// <param name="height">A kép/terület magassága</param>
        /// <param name="addToClipboard">Nem használt, jövőbeni funkciónak fenntartott</param>
        /// <returns>BitmapSource amelyből a kép kinyerhető</returns>
        /// <example>
        /// <code lang='cs'>
        ///   bitmapsource = CaptureRegion(((HwndSource)HwndSource.FromVisual(Application.Current.MainWindow)).Handle,
        ///   (int)Application.Current.MainWindow.Left, (int)Application.Current.MainWindow.Top,
        ///   (int)Application.Current.MainWindow.Width, (int)Application.Current.MainWindow.Height, false);
        /// </code>
        /// </example>
        public static BitmapSource CaptureRegion(
            IntPtr hWnd, int x, int y, int width, int height, bool addToClipboard)
        {
            IntPtr       sourceDC = IntPtr.Zero;
            IntPtr       targetDC = IntPtr.Zero;
            IntPtr       compatibleBitmapHandle = IntPtr.Zero;
            BitmapSource bitmap = null;

            try
            {
                sourceDC = User32.GetDC(User32.GetDesktopWindow());
                targetDC = Gdi32.CreateCompatibleDC(sourceDC);
                compatibleBitmapHandle = Gdi32.CreateCompatibleBitmap(sourceDC, width, height);
                Gdi32.SelectObject(targetDC, compatibleBitmapHandle);
                Gdi32.BitBlt(targetDC, 0, 0, width, height, sourceDC, x, y, Gdi32.SRCCOPY);
                bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    compatibleBitmapHandle, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            }
            catch (Exception ex)
            {
                throw new ScreenCaptureException(string.Format("Error capturing region {0},{1},{2},{3}", x, y, width, height), ex);
            }
            finally
            {
                Gdi32.DeleteObject(compatibleBitmapHandle);
                User32.ReleaseDC(IntPtr.Zero, sourceDC);
                User32.ReleaseDC(IntPtr.Zero, targetDC);
            }

            return(bitmap);
        }
Ejemplo n.º 11
0
        public Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            var hdcSrc = User32.GetWindowDC(handle);
            // get the size
            var windowRect = new User32.Rect();

            User32.GetWindowRect(handle, ref windowRect);
            var width  = windowRect.right - windowRect.left;
            var height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            var hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            var hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.Srccopy);
            // restore selection
            Gdi32.SelectObject(hdcDest, hOld);
            // clean up
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            Gdi32.DeleteObject(hBitmap);
            return(img);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Creates a Bitmap containing the screen of the window
        /// </summary>
        /// <param name="handle">The handle to the window.</param>
        /// <returns>Bitmap containing the screen of the window</returns>
        static private Bitmap CaptureWindowByHandle(IntPtr handle)
        {
            IntPtr hSrc;
            IntPtr hDest;
            IntPtr hBitmap;
            IntPtr hOld;

            //create the rectangle and get image size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int height = windowRect.bottom - windowRect.top;
            int width  = windowRect.right - windowRect.left;

            //get all handle device context
            hSrc    = User32.GetWindowDC(handle);
            hDest   = Gdi32.CreateCompatibleDC(hSrc);
            hBitmap = Gdi32.CreateCompatibleBitmap(hSrc, width, height);
            hOld    = Gdi32.SelectObject(hDest, hBitmap);

            //get the image
            Gdi32.BitBlt(hDest, 0, 0, width, height, hSrc, 0, 0, Gdi32.SRCC);
            Gdi32.SelectObject(hDest, hOld);
            Bitmap img = Image.FromHbitmap(hBitmap);

            //free the memory
            Gdi32.DeleteDC(hDest);
            User32.ReleaseDC(handle, hSrc);
            Gdi32.DeleteObject(hBitmap);

            //return the image
            return(img);
        }
Ejemplo n.º 13
0
        public static Bitmap Capture(IntPtr Window)
        {
            IntPtr SourceDC = User32.GetWindowDC(Window),
                   MemoryDC = Gdi32.CreateCompatibleDC(SourceDC);

            var rect = new RECT();

            User32.GetWindowRect(Window, ref rect);

            int Width  = rect.Right - rect.Left,
                Height = rect.Bottom - rect.Top;

            // Create a bitmap we can copy it to
            var hBmp = Gdi32.CreateCompatibleBitmap(SourceDC, Width, Height);

            try
            {
                // select the bitmap object
                var hOld = Gdi32.SelectObject(MemoryDC, hBmp);

                // bitblt over
                Gdi32.BitBlt(MemoryDC, 0, 0, Width, Height, SourceDC, 0, 0, CopyPixelOperation.SourceCopy);

                // restore selection
                Gdi32.SelectObject(MemoryDC, hOld);

                // get a .NET image object for it
                return(Bitmap.FromHbitmap(hBmp));
            }
            finally { Gdi32.DeleteObject(hBmp); }
        }
Ejemplo n.º 14
0
        //通过鼠标前后两个位置来获取图片
        public Image GetPic_ByMouse(IntPtr hWnd)
        {
            Image img;


            // 获取设备上下文环境句柄
            IntPtr hscrdc = User32.GetWindowDC(hWnd);

            // 创建一个与指定设备兼容的内存设备上下文环境(DC)
            IntPtr hmemdc  = Gdi32.CreateCompatibleDC(hscrdc);
            IntPtr myMemdc = Gdi32.CreateCompatibleDC(hscrdc);

            // 返回指定窗体的矩形尺寸
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(hWnd, ref windowRect);

            // 返回指定设备环境句柄对应的位图区域句柄
            IntPtr myBitmap = Gdi32.CreateCompatibleBitmap(hscrdc, width, height);
            IntPtr hbitmap  = Gdi32.CreateCompatibleBitmap(hscrdc, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top);

            //把位图选进内存DC

            Gdi32.SelectObject(hmemdc, hbitmap);
            Gdi32.SelectObject(myMemdc, myBitmap);


            Gdi32.BitBlt(myMemdc, 0, 0, width, height, hscrdc, Point_push.X, Point_push.Y, Gdi32.SRCCOPY);
            img = Image.FromHbitmap(myBitmap);
            Gdi32.DeleteDC(hscrdc);
            Gdi32.DeleteDC(hmemdc);
            Gdi32.DeleteDC(myMemdc);
            return(img);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Windows only window capture by handle
        /// </summary>
        /// <param name="handle"></param>
        /// <returns></returns>
        public static byte[] WindowsCapturePng(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // get the size
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // create a device context we can copy to
            IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY);
            // restore selection
            Gdi32.SelectObject(hdcDest, hOld);
            // clean up
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            byte[] ret = HbitmapToPng(hBitmap);
            // free up the Bitmap object
            Gdi32.DeleteObject(hBitmap);
            return(ret);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Captures the screen using the SourceCopy | CaptureBlt.
        /// </summary>
        /// <param name="width">The size of the final image.</param>
        /// <param name="height">The size of the final image.</param>
        /// <param name="positionX">Source capture Left position.</param>
        /// <param name="positionY">Source capture Top position.</param>
        /// <returns>A bitmap with the capture rectangle.</returns>
        public static BitmapSource CaptureScreenAsBitmapSource(int width, int height, int positionX, int positionY)
        {
            var hDesk   = User32.GetDesktopWindow();
            var hSrce   = User32.GetWindowDC(hDesk);
            var hDest   = Gdi32.CreateCompatibleDC(hSrce);
            var hBmp    = Gdi32.CreateCompatibleBitmap(hSrce, width, height);
            var hOldBmp = Gdi32.SelectObject(hDest, hBmp);

            try
            {
                var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, positionX, positionY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt);

                //return Image.FromHbitmap(hBmp);
                return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBmp, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
            }
            catch (Exception ex)
            {
                LogWriter.Log(ex, "Impossible to get screenshot of the screen");
            }
            finally
            {
                Gdi32.SelectObject(hDest, hOldBmp);
                Gdi32.DeleteObject(hBmp);
                Gdi32.DeleteDC(hDest);
                User32.ReleaseDC(hDesk, hSrce);
            }

            return(null);
        }
Ejemplo n.º 17
0
        public static Bitmap GetWindow(IntPtr handle, Rectangle rect)
        {
            Bitmap bmp;

            Graphics g    = null;
            IntPtr   dest = IntPtr.Zero;
            IntPtr   src  = IntPtr.Zero;

            try
            {
                bmp = new Bitmap(rect.Width, rect.Height);
                g   = System.Drawing.Graphics.FromImage(bmp);

                dest = g.GetHdc();
                src  = User32.GetDC(handle);

                Gdi32.BitBlt(dest, rect.Left, rect.Top, rect.Width, rect.Height, src, 0, 0,
                             TernaryRasterOperations.CAPTUREBLT | TernaryRasterOperations.SRCCOPY);
            }
            finally
            {
                g.ReleaseHdc(dest);
                //g.ReleaseHdc(dc2);

                g.Dispose();
            }
            return(bmp);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Captures the screen using the SourceCopy | CaptureBlt.
        /// </summary>
        /// <param name="height">Height of the capture region.</param>
        /// <param name="positionX">Source capture Left position.</param>
        /// <param name="positionY">Source capture Top position.</param>
        /// <param name="width">Width of the capture region.</param>
        /// <returns>A bitmap with the capture rectangle.</returns>
        public static Image CaptureScreenAsBitmap(int width, int height, int positionX, int positionY)
        {
            var hDesk   = User32.GetDesktopWindow();
            var hSrce   = User32.GetWindowDC(hDesk);
            var hDest   = Gdi32.CreateCompatibleDC(hSrce);
            var hBmp    = Gdi32.CreateCompatibleBitmap(hSrce, width, height);
            var hOldBmp = Gdi32.SelectObject(hDest, hBmp);

            try
            {
                var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, positionX, positionY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt);

                return(b ? Image.FromHbitmap(hBmp) : null);
            }
            catch (Exception)
            {
                //LogWriter.Log(ex, "Impossible to get screenshot of the screen");
            }
            finally
            {
                Gdi32.SelectObject(hDest, hOldBmp);
                Gdi32.DeleteObject(hBmp);
                Gdi32.DeleteDC(hDest);
                User32.ReleaseDC(hDesk, hSrce);
            }

            return(null);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Captures a specific area from the screen.
        /// </summary>
        public static CaptureImage Rectangle(Rectangle bounds, CaptureSettings settings = null)
        {
            // Calculate the size of the output rectangle
            var outputRectangle = CaptureUtilities.ScaleAccordingToSettings(bounds, settings);

            Bitmap bmp;

            if (outputRectangle.Width == bounds.Width || outputRectangle.Height == bounds.Height)
            {
                // Capture directly without any resizing
                bmp = CaptureDesktopToBitmap(bounds.Width, bounds.Height, (dest, src) =>
                {
                    Gdi32.BitBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
                });
            }
            else
            {
                //  Capture with scaling
                bmp = CaptureDesktopToBitmap(outputRectangle.Width, outputRectangle.Height, (dest, src) =>
                {
                    Gdi32.SetStretchBltMode(dest, StretchMode.STRETCH_HALFTONE);
                    Gdi32.StretchBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, bounds.Width, bounds.Height, TernaryRasterOperations.SRCCOPY | TernaryRasterOperations.CAPTUREBLT);
                });
            }
            return(new CaptureImage(bmp, bounds, settings));
        }
Ejemplo n.º 20
0
        public static Image CaptureWindow(IntPtr handle, double scale)
        {
            var rectangle = Windows.GetWindowRect(handle);
            var posX      = (int)((rectangle.X + Util.Constants.LeftOffset) * scale);
            var posY      = (int)((rectangle.Y + Util.Constants.TopOffset) * scale);
            var width     = (int)((rectangle.Width - Util.Constants.HorizontalOffset) * scale);
            var height    = (int)((rectangle.Height - Util.Constants.VerticalOffset) * scale);

            var hDesk   = User32.GetDesktopWindow();
            var hSrce   = User32.GetWindowDC(hDesk);
            var hDest   = Gdi32.CreateCompatibleDC(hSrce);
            var hBmp    = Gdi32.CreateCompatibleBitmap(hSrce, width, height);
            var hOldBmp = Gdi32.SelectObject(hDest, hBmp);

            var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, posX, posY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt);

            try
            {
                return(Image.FromHbitmap(hBmp));
            }
            catch (Exception ex)
            {
                LogWriter.Log(ex, "Impossible to get screenshot of the screen");
            }
            finally
            {
                Gdi32.SelectObject(hDest, hOldBmp);
                Gdi32.DeleteObject(hBmp);
                Gdi32.DeleteDC(hDest);
                User32.ReleaseDC(hDesk, hSrce);
            }

            return(null);
        }
Ejemplo n.º 21
0
		public static Image CaptureDesktopRegion( Point location, Size size ) {
				
			Image myImage = new Bitmap( size.Width, size.Height );

			using ( Graphics g = Graphics.FromImage( myImage ) ) {

				IntPtr destDeviceContext = g.GetHdc();
				IntPtr srcDeviceContext = User32.GetWindowDC( IntPtr.Zero ); // capture desktop
						
				// TODO: throw exception
				int result = Gdi32.BitBlt( destDeviceContext, 0, 0, size.Width, size.Height, srcDeviceContext, location.X, location.Y, RasterOperationCodes.SRCCOPY );

				if ( result == 0 ) {

					// TODO: call GetLastError to dig down to the core of the problem.
					throw new ImageManipulationException( "There was a problem with the BitBlt function call." );

				}
		
				User32.ReleaseDC( IntPtr.Zero, srcDeviceContext );
				g.ReleaseHdc( destDeviceContext );

			} // dispose the Graphics object

			return myImage;

		}
Ejemplo n.º 22
0
 public void Clear(int x, int y, int width, int height)
 {
     if (!Gdi32.BitBlt(DeviceContext, x, y, width, height, IntPtr.Zero, 0, 0, Gdi32.RasterOperation.BLACKNESS))
     {
         throw new Win32Exception("BitBlt failed.");
     }
 }
Ejemplo n.º 23
0
        // 特定窗口的截图对象
        private Image CaptureWindow(IntPtr handle)
        {
            // 获得目标窗口的hDC
            SafeDCHandle hdcSrc     = User32.GetWindowDC(handle);
            var          screenSize = GetScreenPhysicalSzie();
            // create a device context we can copy to
            var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, screenSize.Width, screenSize.Height);
            // select the bitmap object
            IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            Gdi32.BitBlt(hdcDest.HWnd, 0, 0, screenSize.Width, screenSize.Height, hdcSrc.HWnd, 0, 0, WindowsAPIUtils.SRCCOPY);
            // restore selection
            Gdi32.SelectObject(hdcDest, hOld);
            // clean up
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc.HWnd);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            Gdi32.DeleteObject(hBitmap);
            return(img);
        }
Ejemplo n.º 24
0
        private Image CaptureWindow(IntPtr handle)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            RECT windowRect = new RECT();

            User32.GetWindowRect(handle, out windowRect);
            int width  = windowRect.Right - windowRect.Left;
            int height = windowRect.Bottom - windowRect.Top;
            // create a device context we can copy to
            IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            // bitblt over
            Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY);
            // restore selection
            Gdi32.SelectObject(hdcDest, hOld);
            // clean up
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            Gdi32.DeleteObject(hBitmap);
            return(img);
        }
Ejemplo n.º 25
0
        private Gdi32.HBITMAP GetCompatibleBitmap(Bitmap bm)
        {
            using var screenDC = User32.GetDcScope.ScreenDC;

            // GDI+ returns a DIBSECTION based HBITMAP. The clipboard deals well
            // only with bitmaps created using CreateCompatibleBitmap(). So, we
            // convert the DIBSECTION into a compatible bitmap.
            Gdi32.HBITMAP hBitmap = bm.GetHBITMAP();

            // Create a compatible DC to render the source bitmap.
            using var sourceDC = new Gdi32.CreateDcScope(screenDC);
            using var sourceBitmapSelection = new Gdi32.SelectObjectScope(sourceDC, hBitmap);

            // Create a compatible DC and a new compatible bitmap.
            using var destinationDC = new Gdi32.CreateDcScope(screenDC);
            Gdi32.HBITMAP bitmap = Gdi32.CreateCompatibleBitmap(screenDC, bm.Size.Width, bm.Size.Height);

            // Select the new bitmap into a compatible DC and render the blt the original bitmap.
            using var destinationBitmapSelection = new Gdi32.SelectObjectScope(destinationDC, bitmap);
            Gdi32.BitBlt(
                destinationDC,
                0,
                0,
                bm.Size.Width,
                bm.Size.Height,
                sourceDC,
                0,
                0,
                Gdi32.ROP.SRCCOPY);

            return(bitmap);
        }
Ejemplo n.º 26
0
        public Bitmap CaptureWindow(IntPtr handle)
        {
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);

            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

            IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);

            Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.SRCCOPY);
            Gdi32.SelectObject(hdcDest, hOld);
            Gdi32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);

            Image image = Image.FromHbitmap(hBitmap);

            Gdi32.DeleteObject(hBitmap);

            return(new Bitmap(image));
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Captures a Specific Window.
        /// </summary>
        /// <param name="Window">The <see cref="Window"/> to Capture</param>
        /// <param name="IncludeCursor">Whether to include the Mouse Cursor.</param>
        /// <returns>The Captured Image.</returns>
        public static Bitmap Capture(Window Window, bool IncludeCursor = false)
        {
            User32.GetWindowRect(Window.Handle, out var r);
            var region = r.ToRectangle();

            IntPtr hSrc    = GetWindowDC(Window.Handle),
                   hDest   = Gdi32.CreateCompatibleDC(hSrc),
                   hBmp    = Gdi32.CreateCompatibleBitmap(hSrc, region.Width, region.Height),
                   hOldBmp = Gdi32.SelectObject(hDest, hBmp);

            Gdi32.BitBlt(hDest, 0, 0,
                         region.Width, region.Height,
                         hSrc,
                         region.Left, region.Top,
                         CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

            var bmp = Image.FromHbitmap(hBmp);

            Gdi32.SelectObject(hDest, hOldBmp);
            Gdi32.DeleteObject(hBmp);
            Gdi32.DeleteDC(hDest);
            Gdi32.DeleteDC(hSrc);

            var clone = bmp.Clone(new Rectangle(Point.Empty, bmp.Size), PixelFormat.Format24bppRgb);

            if (IncludeCursor)
            {
                new MouseCursor().Draw(clone, region.Location);
            }

            return(clone);
        }
Ejemplo n.º 28
0
        public static Bitmap CopyImage(Control control, int x, int y, int width, int height)
        {
            if (control == null)
            {
                return(null);
            }
            IntPtr handle = control.Handle;

            if (handle == IntPtr.Zero)
            {
                return(null);
            }

            IntPtr wdc = User32.GetWindowDC(handle);

            if (wdc == IntPtr.Zero)
            {
                return(null);
            }

            Bitmap bmp = new Bitmap(width, height);

            using (Graphics grf = Graphics.FromImage(bmp))
            {
                IntPtr dc = grf.GetHdc();
                Gdi32.BitBlt(dc, 0, 0, width, height, wdc, x, y, ROP2.SRCCOPY | ROP2.CAPTUREBLT);
            }

            User32.ReleaseDC(handle, wdc);

            return(bmp);
        }
Ejemplo n.º 29
0
        private static Image CaptureWindow(IntPtr handler)
        {
            IntPtr hdcScr     = User32.GetWindowDC(handler);
            var    windowRect = new User32.RECT();

            User32.GetWindowRect(handler, ref windowRect);

            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;

            var hdcDesc = Gdi32.CreateCompatibleDC(hdcScr);
            var hBitmap = Gdi32.CreateCompatibleBitmap(hdcScr, width, height);

            var hOld = Gdi32.SelectObject(hdcDesc, hBitmap);

            Gdi32.BitBlt(hdcDesc, 0, 0, width, height, hdcScr, 0, 0, Gdi32.SRCCOPY);

            Gdi32.SelectObject(hdcDesc, hOld);
            Gdi32.DeleteDC(hdcDesc);
            User32.ReleaseDC(handler, hdcScr);

            var img = Image.FromHbitmap(hBitmap);

            Gdi32.DeleteObject(hBitmap);
            return(img);
        }
Ejemplo n.º 30
0
            public void Overwrite(
                Gdi32.DeviceContextSafeHandle bitmapDC,
                Gdi32.DeviceContextSafeHandle windowDC,
                int windowClientLeft,
                int windowClientTop,
                int windowClientWidth,
                int windowClientHeight,
                uint windowDpi,
                uint zOrder)
            {
                if (windowClientWidth > 0 && windowClientHeight > 0)
                {
                    if (bitmap is null || bitmapWidth < windowClientWidth || bitmapHeight < windowClientHeight)
                    {
                        if (bitmap is null)
                        {
                            // Most of the time, windows don't resize, so save some space by not rounding up.
                            bitmapWidth  = windowClientWidth;
                            bitmapHeight = windowClientHeight;
                        }
                        else
                        {
                            // Round up to the nearest 256 pixels to minimize the number of times that bitmaps are
                            // reallocated.
                            bitmapWidth  = ((Math.Max(bitmapWidth, windowClientWidth) + 255) / 256) * 256;
                            bitmapHeight = ((Math.Max(bitmapHeight, windowClientHeight) + 255) / 256) * 256;

                            bitmap.Dispose();
                        }

                        bitmap = Gdi32.CreateDIBSection(windowDC, new()
                        {
                            bmiHeader =
                            {
                                biSize     = Marshal.SizeOf <Gdi32.BITMAPINFOHEADER>(),
                                biWidth    = bitmapWidth,
                                biHeight   =                             -bitmapHeight,
                                biPlanes   =                                         1,
                                biBitCount = BitsPerPixel,
                            },
                        }, Gdi32.DIB.RGB_COLORS, ppvBits: out _, hSection: IntPtr.Zero, offset: 0).ThrowLastErrorIfInvalid();
                    }

                    Gdi32.SelectObject(bitmapDC, bitmap).ThrowWithoutLastErrorAvailableIfInvalid(nameof(Gdi32.SelectObject));

                    if (!Gdi32.BitBlt(bitmapDC, 0, 0, windowClientWidth, windowClientHeight, windowDC, 0, 0, Gdi32.RasterOperation.SRCCOPY))
                    {
                        throw new Win32Exception();
                    }
                }

                WindowClientLeft   = windowClientLeft;
                WindowClientTop    = windowClientTop;
                WindowClientWidth  = windowClientWidth;
                WindowClientHeight = windowClientHeight;
                WindowDpi          = windowDpi;
                ZOrder             = zOrder;
            }