Esempio n. 1
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));
        }
            void IDisposable.Dispose()
            {
                if (HDC == IntPtr.Zero || _hMetafileDC == IntPtr.Zero || _hBitmap == IntPtr.Zero)
                {
                    return;
                }

                bool success;

                try
                {
                    success = DICopy(_hMetafileDC, HDC, _destRect, true);
                    Debug.Assert(success, "DICopy() failed.");
                    Gdi32.SelectObject(HDC, _hOriginalBmp);
                    success = Gdi32.DeleteObject(_hBitmap).IsTrue();
                    Debug.Assert(success, "DeleteObject() failed.");
                    success = Gdi32.DeleteDC(HDC);
                    Debug.Assert(success, "DeleteObject() failed.");
                }
                finally
                {
                    // Dispose is done. Set all the handles to IntPtr.Zero so this way the Dispose method executes only once.
                    HDC           = IntPtr.Zero;
                    _hBitmap      = IntPtr.Zero;
                    _hOriginalBmp = IntPtr.Zero;

                    GC.SuppressFinalize(this);
                }
            }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
        public void DiscardDeviceResources()
        {
            _renderTarget?.Dispose();
            _renderTarget = null;

            if (_memDC != IntPtr.Zero && _hBitmap != IntPtr.Zero && _hOldBitmap != IntPtr.Zero)
            {
                Gdi32.SelectObject(_memDC, _hOldBitmap);
                Gdi32.DeleteObject(_hBitmap);
            }

            if (_memDC != IntPtr.Zero)
            {
                Gdi32.DeleteDC(_memDC);
            }

            if (_screenDC != IntPtr.Zero)
            {
                User32.ReleaseDC(IntPtr.Zero, _screenDC);
            }

            _hOldBitmap = IntPtr.Zero;
            _hBitmap    = IntPtr.Zero;
            _memDC      = IntPtr.Zero;
            _screenDC   = IntPtr.Zero;

            GC.Collect();
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        protected void UpdateLayeredWindow(Point point, Size size, byte alpha, bool wrap)
        {
            // Create bitmap for drawing onto
            Bitmap memoryBitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);

            using (Graphics g = Graphics.FromImage(memoryBitmap))
            {
                Rectangle area = new Rectangle(0, 0, size.Width, size.Height);

                // Draw the background area
                Draw(g, area, wrap);

                // Get hold of the screen DC
                IntPtr hDC = User32.GetDC(IntPtr.Zero);

                // Create a memory based DC compatible with the screen DC
                IntPtr memoryDC = Gdi32.CreateCompatibleDC(hDC);

                // Get access to the bitmap handle contained in the Bitmap object
                IntPtr hBitmap = memoryBitmap.GetHbitmap(Color.FromArgb(0));

                // Select this bitmap for updating the window presentation
                IntPtr oldBitmap = Gdi32.SelectObject(memoryDC, hBitmap);

                // New window size
                SIZE ulwsize;
                ulwsize.cx = size.Width;
                ulwsize.cy = size.Height;

                // New window position
                POINT topPos;
                topPos.x = point.X;
                topPos.y = point.Y;

                // Offset into memory bitmap is always zero
                POINT pointSource;
                pointSource.x = 0;
                pointSource.y = 0;

                // We want to make the entire bitmap opaque
                BLENDFUNCTION blend = new BLENDFUNCTION();
                blend.BlendOp             = (byte)AlphaFlags.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = alpha;
                blend.AlphaFormat         = (byte)AlphaFlags.AC_SRC_ALPHA;

                // Tell operating system to use our bitmap for painting
                User32.UpdateLayeredWindow(Handle, hDC, ref topPos, ref ulwsize,
                                           memoryDC, ref pointSource, 0, ref blend,
                                           (int)UpdateLayeredWindowsFlags.ULW_ALPHA);

                // Put back the old bitmap handle
                Gdi32.SelectObject(memoryDC, oldBitmap);

                // Cleanup resources
                User32.ReleaseDC(IntPtr.Zero, hDC);
                Gdi32.DeleteObject(hBitmap);
                Gdi32.DeleteDC(memoryDC);
            }
        }
Esempio n. 10
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);
        }
Esempio n. 11
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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
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);
        }
Esempio n. 14
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);
            }
        }
Esempio n. 15
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);
        }
Esempio n. 16
0
        private void Render()
        {
            RECT rect = new RECT();

            User32.GetWindowRect(_handle, ref rect);

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

            if (width == 0 || height == 0)
            {
                return;
            }

            POINT  newLocation = new POINT(rect.left, rect.top);
            SIZE   newSize     = new SIZE(width, height);
            IntPtr screenDc    = User32.GetDC(IntPtr.Zero);
            IntPtr memDc       = Gdi32.CreateCompatibleDC(screenDc);

            using (Bitmap bmp = GetBitmap(width, height))
            {
                IntPtr hBitmap    = bmp.GetHbitmap(_transparent);
                IntPtr hOldBitmap = Gdi32.SelectObject(memDc, hBitmap);

                User32.UpdateLayeredWindow(_handle, screenDc, ref newLocation, ref newSize, memDc, ref _ptZero, 0, ref _blend, 0x02);

                User32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Gdi32.SelectObject(memDc, hOldBitmap);
                    Gdi32.DeleteObject(hBitmap);
                }
            }
            Gdi32.DeleteDC(memDc);
        }
Esempio n. 17
0
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            hBitmap   = IntPtr.Zero;
            oldBitmap = IntPtr.Zero;
            topPos    = Location;
            bmpbase   = bitmap.Clone() as Bitmap;
            Bounds    = new Rectangle(Location, bmpbase.Size);

            Size size = bmpbase.Size;

            wDC   = User32.GetDC(this.Handle);           // was IntPtr.Zero
            memDC = Gdi32.CreateCompatibleDC(wDC);

            try
            {
                hBitmap   = bmpbase.GetHbitmap(Color.FromArgb(0));                // Color.FromArgb(0);grab a GDI handle from this GDI+ bitmap
                oldBitmap = Gdi32.SelectObject(memDC, hBitmap);
                Point pointSource = new Point(0, 0);

                BlendUtil.LayeredAlphaBlend(opacity, Handle, wDC, memDC, ref topPos, ref size, ref pointSource);
                OnMove(null);
            }
            finally {
                User32.ReleaseDC(IntPtr.Zero, wDC);
                if (hBitmap != IntPtr.Zero)
                {
                    Gdi32.SelectObject(memDC, oldBitmap);
                    //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win GDI and it's working fine without any resource leak.
                    Gdi32.DeleteObject(hBitmap);
                }
                Gdi32.DeleteDC(memDC);
            }
        }
Esempio n. 18
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);
        }
Esempio n. 19
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);
        }
Esempio 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);
        }
Esempio n. 21
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);
        }
Esempio n. 22
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);
        }
Esempio n. 23
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);
        }
Esempio n. 24
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);
        }
Esempio n. 25
0
        /// <summary>
        /// Invalidates the specified region of the window.
        /// </summary>
        public new void Invalidate(Rectangle rect)
        {
            if (rect.Width < 1 || rect.Height < 1)
            {
                return;
            }

            Bitmap memoryBitmap = new Bitmap(
                rect.Size.Width,
                rect.Size.Height,
                PixelFormat.Format32bppArgb
                );

            using (Graphics g = Graphics.FromImage(memoryBitmap))
            {
                Rectangle area = new Rectangle(0, 0, rect.Size.Width, rect.Size.Height);
                this.RaisePaintEvent(g, area);
                IntPtr hDC       = User32.GetDC(IntPtr.Zero);
                IntPtr memoryDC  = Gdi32.CreateCompatibleDC(hDC);
                IntPtr hBitmap   = memoryBitmap.GetHbitmap(Color.FromArgb(0));
                IntPtr oldBitmap = Gdi32.SelectObject(memoryDC, hBitmap);

                SIZE size;
                size.cx = rect.Size.Width;
                size.cy = rect.Size.Height;

                POINT location;
                location.x = rect.Location.X;
                location.y = rect.Location.Y;

                POINT sourcePoint;
                sourcePoint.x = 0;
                sourcePoint.y = 0;

                BLENDFUNCTION blend = new BLENDFUNCTION();
                blend.AlphaFormat         = (byte)WinGdi.AC_SRC_ALPHA;
                blend.BlendFlags          = 0;
                blend.BlendOp             = (byte)WinGdi.AC_SRC_OVER;
                blend.SourceConstantAlpha = (byte)(255 - ((this.Opacity * 255) / 100));

                User32.UpdateLayeredWindow(
                    this.Handle,
                    hDC,
                    ref location,
                    ref size,
                    memoryDC,
                    ref sourcePoint,
                    0,
                    ref blend,
                    WinUser.ULW_ALPHA
                    );

                Gdi32.SelectObject(memoryDC, oldBitmap);

                User32.ReleaseDC(IntPtr.Zero, hDC);
                Gdi32.DeleteObject(hBitmap);
                Gdi32.DeleteDC(memoryDC);
            }
        }
Esempio n. 26
0
        private void UpdateBitmap()
        {
            using (Bitmap bitmap = CreateBitmap())
            {
                IntPtr screenDC = User32.GetDC(IntPtr.Zero);
                try
                {
                    IntPtr memDC = Gdi32.CreateCompatibleDC(screenDC);
                    try
                    {
                        IntPtr hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                        try
                        {
                            IntPtr hOrigBitmap = Gdi32.SelectObject(memDC, hBitmap);
                            try
                            {
                                POINT dst = new POINT();
                                dst.x = Left;
                                dst.y = Top;

                                SIZE size = new SIZE();
                                size.cx = bitmap.Width;
                                size.cy = bitmap.Height;

                                POINT src = new POINT();
                                src.x = 0;
                                src.y = 0;

                                User32.BLENDFUNCTION blendFunction = new User32.BLENDFUNCTION();
                                blendFunction.BlendOp             = 0; // AC_SRC_OVER
                                blendFunction.BlendFlags          = 0;
                                blendFunction.SourceConstantAlpha = 255;
                                blendFunction.AlphaFormat         = 1; // AC_SRC_ALPHA

                                User32.UpdateLayeredWindow(Handle, screenDC, ref dst, ref size, memDC, ref src, 0, ref blendFunction, 2);
                            }
                            finally
                            {
                                Gdi32.SelectObject(memDC, hOrigBitmap);
                            }
                        }
                        finally
                        {
                            Gdi32.DeleteObject(hBitmap);
                        }
                    }
                    finally
                    {
                        Gdi32.DeleteDC(memDC);
                    }
                }
                finally
                {
                    User32.ReleaseDC(IntPtr.Zero, screenDC);
                }
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">A <see cref="IntPtr"/> to the window to capture.
        /// (In windows forms, this is obtained by the Handle property)</param>
        /// <returns>An <see cref="Image"/> with the captured window if successfull,
        /// otherwise null.</returns>
        /// <remarks>This method uses the GDI32.BitBlt()
        /// method, so the window has to be visible and should not be
        /// minimized or hidden for example. In this cases use
        /// <see cref="GetWindowImage(IntPtr,Size)"/> which uses
        /// User32.PrintWindow().
        /// </remarks>
        public static Image CaptureWindow(IntPtr handle)
        {
            IntPtr hdcSrc  = IntPtr.Zero;
            IntPtr hdcDest = IntPtr.Zero;
            IntPtr bitmap  = IntPtr.Zero;
            Image  img;

            try
            {
                // get te hDC of the target window
                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
                hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);

                // create a bitmap we can copy it to,
                // using GetDeviceCaps to get the width/height
                bitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

                // select the bitmap object
                IntPtr old = Gdi32.SelectObject(hdcDest, bitmap);

                // bitblt over
                Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.TernaryRasterOperations.SRCCOPY);

                // restore selection
                Gdi32.SelectObject(hdcDest, old);

                // get a .NET image object for it
                img = Image.FromHbitmap(bitmap);
            }
            catch (Exception ex)
            {
                VGExceptionMethods.HandleException(ex);

                return(null);
            }
            finally
            {
                // clean up
                Gdi32.DeleteDC(hdcDest);
                User32.ReleaseDC(handle, hdcSrc);

                // free up the Bitmap object
                Gdi32.DeleteObject(bitmap);
            }

            return(img);
        }
Esempio n. 28
0
        internal void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            Disposing?.Invoke(this, EventArgs.Empty);

            _disposed = true;

            DisposeFont(disposing);

            switch (DeviceContextType)
            {
            case DeviceContextType.Display:
                Debug.Assert(disposing, "WARNING: Finalizing a Display DeviceContext.\r\nReleaseDC may fail when not called from the same thread GetDC was called from.");

                ((IDeviceContext)this).ReleaseHdc();
                break;

            case DeviceContextType.Information:
            case DeviceContextType.NamedDevice:

                // CreateDC and CreateIC add an HDC handle to the HandleCollector; to remove it properly we need
                // to call DeleteHDC.
#if TRACK_HDC
                Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DC.DeleteHDC(hdc=0x{0:x8})", unchecked ((int)this.hDC))));
#endif

                Gdi32.DeleteDC(_hDC);
                _hDC = IntPtr.Zero;
                break;

            case DeviceContextType.Memory:

                // CreatCompatibleDC adds a GDI handle to HandleCollector, to remove it properly we need to call
                // DeleteDC.
#if TRACK_HDC
                Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DC.DeleteDC(hdc=0x{0:x8})", unchecked ((int)this.hDC))));
#endif
                Gdi32.DeleteDC(_hDC);
                _hDC = IntPtr.Zero;
                break;

            case DeviceContextType.Unknown:
            default:
                return;
                // do nothing, the hdc is not owned by this object.
                // in this case it is ok if disposed throught finalization.
            }

            DbgUtil.AssertFinalization(this, disposing);
        }
Esempio n. 29
0
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            bmpbase = bitmap.Clone() as Bitmap;
            if (HasChildren && DoDrawChildren)
            {
                DrawChildren(bmpbase);
            }

            /*
             * if (bitmap.PixelFormat != PixelFormat.Format32bppPArgb)
             *      throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
             */
            // The ideia of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.
            Bounds    = new Rectangle(Location, bmpbase.Size);
            Size      = bmpbase.Size;
            screenDc  = User32.GetDC(this.Handle);            // was IntPtr.Zero
            memDc     = Gdi32.CreateCompatibleDC(screenDc);
            hBitmap   = IntPtr.Zero;
            oldBitmap = IntPtr.Zero;

            //ClientSize = bitmap.Size;

            try {
                hBitmap   = bmpbase.GetHbitmap(Color.FromArgb(0));                // Color.FromArgb(0);grab a GDI handle from this GDI+ bitmap
                oldBitmap = Gdi32.SelectObject(memDc, hBitmap);
                Size  size                = new Size(bmpbase.Width, bmpbase.Height);
                Point pointSource         = new Point(0, 0);
                Point topPos              = Location;
                Gdi32.BLENDFUNCTION blend = new Gdi32.BLENDFUNCTION();
                blend.BlendOp             = Gdi32.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat         = Gdi32.AC_SRC_ALPHA;

                User32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Gdi32.ULW_ALPHA);
            }
            finally {
                User32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Gdi32.SelectObject(memDc, oldBitmap);
                    //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win GDI and it's working fine without any resource leak.
                    Gdi32.DeleteObject(hBitmap);
                }
                Gdi32.DeleteDC(memDc);
            }
        }
Esempio n. 30
0
        /*public Image DrawToBitmap(IntPtr hWnd)
         * {
         *  Bitmap image = new Bitmap(this.ClientSize.Width, this.ClientSize.Height,
         *      System.Drawing.Imaging.PixelFormat.Format32bppArgb);
         *
         *  using (Graphics graphics = Graphics.FromImage(image))
         *  {
         *      IntPtr hdcFrom = WindowsApi.GetDC(hWnd);
         *      WindowsApi.SendMessage(hWnd, WindowsApi.WM_PRINT, hdcFrom, WindowsApi.PRF_CLIENT | WindowsApi.PRF_CHILDREN | WindowsApi.PRF_ERASEBKGND);
         *      graphics.ReleaseHdc(hdcFrom);
         *  }
         *  return image;
         * }*/

        public static Bitmap createBitmap(IntPtr hWnd)
        {
            Bitmap bmp = null;
            //IntPtr hdcWindow = WindowsApi.GetDC(hWnd);
            IntPtr hdcWindow = User32.GetWindowDC(hWnd);
            IntPtr hmbWindow = Gdi32.CreateCompatibleDC(hdcWindow);

            WinDef.RECT rcWindow;
            User32.GetWindowRect(hWnd, out rcWindow);
            WinDef.RECT rcClient;
            User32.GetClientRect(hWnd, out rcClient);

            // stretch screen in to windowUser32
            //WindowsApi.SetStretchBltMode(hdcWindow, WindowsApi.STRETCH_HALFTONE);

            //The source DC is the entire screen and the destination DC is the current window (HWND)
            //WindowsApi.StretchBlt(hdcWindow,
            //    0, 0,
            //    rcWindow.Right, rcWindow.Bottom,
            //    hdcScreen,
            //    0, 0,
            //    WindowsApi.GetSystemMetrics(WindowsApi.SystemMetric.SM_CXSCREEN),
            //    WindowsApi.GetSystemMetrics(WindowsApi.SystemMetric.SM_CYSCREEN),
            //    WindowsApi.SRCCOPY);

            //X and Y coordinates of window
            IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcWindow, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top);

            if (hBitmap != IntPtr.Zero)
            {
                // adjust and copy
                IntPtr hLocalBitmap = Gdi32.SelectObject(hmbWindow, hBitmap);
                User32.SetActiveWindow(hWnd);
                User32.SendMessage(hWnd, WinUser.WindowMessage.WM_PRINT, hdcWindow, WinUser.PrintFlags.PRF_NONCLIENT | WinUser.PrintFlags.PRF_CLIENT | WinUser.PrintFlags.PRF_CHILDREN | WinUser.PrintFlags.PRF_ERASEBKGND);
                Gdi32.BitBlt(hmbWindow, 0, 0, rcWindow.Right - rcWindow.Left, rcWindow.Bottom - rcWindow.Top, hdcWindow, 0, 0, WinGdi.TernaryRasterOperations.SRCCOPY);
                //WindowsApi.PrintWindow(hWnd, hBitmap, 0);

                Gdi32.SelectObject(hmbWindow, hLocalBitmap);
                //We delete the memory device context.
                Gdi32.DeleteDC(hmbWindow);
                //We release the screen device context.
                User32.ReleaseDC(hWnd, hdcWindow);
                //Image is created by Image bitmap handle and assigned to Bitmap variable.
                bmp = System.Drawing.Image.FromHbitmap(hBitmap);
                //Delete the compatible bitmap object.
                Gdi32.DeleteObject(hBitmap);
                //bmp.Save("C:\\Users\\bjcullinan\\Documents\\Visual Studio 2012\\Projects\\Appifier\\Appifier\\bin\\Debug\\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            }
            return(bmp);
        }