예제 #1
0
        /// <summary>
        /// Gets a segment of the desktop as an image.
        /// </summary>
        /// <returns>A <see cref="System.Drawing.Image"/> containg an image of the full desktop.</returns>
        public Image GetDesktopBitmapBg(IntPtr hWnd, bool forcePrintWindow = false, bool forceBitBlt = false)
        {
            var rect = new User32.RECT();

            User32.GetClientRect(hWnd, ref rect);

            bool dwmEnabled;

            DWM.DwmIsCompositionEnabled(out dwmEnabled);
            if ((!dwmEnabled && !forcePrintWindow) || forceBitBlt)
            {
                return(this.GetDesktopBitmap(hWnd, rect));
            }

            var img = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppRgb);

            using (var g = Graphics.FromImage(img))
            {
                IntPtr dc = g.GetHdc();
                // User32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, User32.RedrawWindowFlags.Frame | User32.RedrawWindowFlags.Invalidate | User32.RedrawWindowFlags.Erase | User32.RedrawWindowFlags.UpdateNow | User32.RedrawWindowFlags.AllChildren);
                bool success = User32.PrintWindow(hWnd, dc, 1);
                g.ReleaseHdc(dc);
                GDI32.DeleteDC(dc);
                if (!success && !forcePrintWindow)
                {
                    return(this.GetDesktopBitmap(hWnd, rect));
                }
                if (!forcePrintWindow && img.Width > 64 && img.Height > 64 && img.IsAllBlack())
                {
                    return(this.GetDesktopBitmap(hWnd, rect));
                }
            }
            return(img);
        }
예제 #2
0
  //===================================================================
  // スクリーンキャプチャ
  //===================================================================

  /// スクリーンキャプチャした結果をHBitmapに格納する
  /// @warning 返り値はかならずusingと一緒に使うかDispose()すること
  /// @return スクリーンキャプチャした結果のHBitmap
  public BitmapHandle Execute() {
    // Windowチェック
    if (!Common.Utilities.IsWindowValid(this.Window)) return null;

    // BitBlt
    var window = this.Window;
    var windowDC = User32.GetDC(window);
    var capturedDC = GDI32.CreateCompatibleDC(windowDC);
    var capturedBitmap = GDI32.CreateCompatibleBitmap(windowDC,
        this.ClippingWidth, this.ClippingHeight);
    {
      var originalBitmap = GDI32.SelectObject(capturedDC, capturedBitmap);
      GDI32.BitBlt(capturedDC,
                   0, 0, this.ClippingWidth, this.ClippingHeight,
                   windowDC,
                   this.ClippingX, this.ClippingY,
                   this.ShowLayeredWindow ? GDI32.SRCCOPY | GDI32.CAPTUREBLT
                                          : GDI32.SRCCOPY);
      GDI32.SelectObject(capturedDC, originalBitmap);
    }    
    GDI32.DeleteDC(capturedDC);
    User32.ReleaseDC(window, windowDC);

    /// @todo(me) マウスカーソルの合成・・・?いるか?
    if (this.ShowCursor) {
      // nop
    }

    return new BitmapHandle(capturedBitmap);
  }
예제 #3
0
        /// <summary>
        /// Retrieves an image of the specified part of your screen.
        /// </summary>
        /// <param name="x">The X coordinate of the requested area</param>
        /// <param name="y">The Y coordinate of the requested area</param>
        /// <param name="width">The width of the requested area</param>
        /// <param name="height">The height of the requested area</param>
        /// <returns>A <see cref="System.Drawing.Image"/> of the desktop at
        /// the specified coordinates.</returns>
        public Image GetDesktopBitmap(int x, int y, int width, int height)
        {
            //Create the image and graphics to capture the portion of the desktop.
            Image destinationImage = new Bitmap(width, height, PixelFormat.Format32bppRgb);

            using (Graphics destinationGraphics = Graphics.FromImage(destinationImage))
            {
                IntPtr destinationGraphicsHandle = IntPtr.Zero;
                IntPtr windowDC = IntPtr.Zero;

                try
                {
                    //Pointers for window handles
                    destinationGraphicsHandle = destinationGraphics.GetHdc();
                    windowDC = User32.GetDC(IntPtr.Zero);

                    //Get the screencapture
                    var dwRop = GDI32.TernaryRasterOperations.SRCCOPY;

                    GDI32.BitBlt(destinationGraphicsHandle, 0, 0, width, height, windowDC, x, y, dwRop);
                }
                finally
                {
                    destinationGraphics.ReleaseHdc(destinationGraphicsHandle);
                    GDI32.DeleteDC(windowDC);
                }
            }

            // Don't forget to dispose this image
            return(destinationImage);
        }
예제 #4
0
        public static Bitmap CaptureScreen()
        {
            IntPtr handle = User32.GetDesktopWindow();
            IntPtr hdcSrc = User32.GetWindowDC(handle);

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

            if ((Screen.PrimaryScreen.WorkingArea.Width == 1536) && (Screen.PrimaryScreen.WorkingArea.Height == 824))
            {
                width  = 1920;
                height = 1080;
            }
            else
            {
                width  = (Screen.PrimaryScreen.WorkingArea.Width);
                height = (Screen.PrimaryScreen.WorkingArea.Height);
            }
            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);
            Bitmap img = Image.FromHbitmap(hBitmap);

            GDI32.DeleteObject(hBitmap);
            GC.Collect();
            return(img);
        }
예제 #5
0
        public Image CaptureWindow(IntPtr handle)
        {
            IntPtr windowDC = User32.GetWindowDC(handle);

            User32.RECT rect = new User32.RECT();

            User32.GetWindowRect(handle, ref rect);

            int nWidth = rect.right - rect.left;

            int nHeight = rect.bottom - rect.top;

            IntPtr hDC = GDI32.CreateCompatibleDC(windowDC);

            IntPtr hObject = GDI32.CreateCompatibleBitmap(windowDC, nWidth, nHeight);

            IntPtr ptr4 = GDI32.SelectObject(hDC, hObject);

            GDI32.BitBlt(hDC, 0, 0, nWidth, nHeight, windowDC, 0, 0, 0xcc0020);

            GDI32.SelectObject(hDC, ptr4);

            GDI32.DeleteDC(hDC);

            User32.ReleaseDC(handle, windowDC);

            Image image = Image.FromHbitmap(hObject);

            GDI32.DeleteObject(hObject);

            return(image);
        }
예제 #6
0
        public static Bitmap getRegion(Rectangle rect)
        {
            int    x      = rect.X;
            int    y      = rect.Y;
            int    width  = rect.Width;
            int    height = rect.Height;
            IntPtr hBmp;
            IntPtr hdcScreen     = User32.GetDC(User32.GetDesktopWindow());
            IntPtr hdcCompatible = GDI32.CreateCompatibleDC(hdcScreen);

            hBmp = GDI32.CreateCompatibleBitmap(hdcScreen, width, height);

            if (hBmp != IntPtr.Zero)
            {
                IntPtr hOldBmp = (IntPtr)GDI32.SelectObject(hdcCompatible, hBmp);
                GDI32.BitBlt(hdcCompatible, 0, 0, width, height, hdcScreen, x, y, GDI32.TernaryRasterOperations.SRCCOPY);

                GDI32.SelectObject(hdcCompatible, hOldBmp);
                GDI32.DeleteDC(hdcCompatible);
                User32.ReleaseDC(User32.GetDesktopWindow(), hdcScreen);

                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);

                GDI32.DeleteObject(hBmp);
                GC.Collect();

                return(bmp);
            }

            return(null);
        }
예제 #7
0
        public Bitmap CaptureRegion(IntPtr handle, int x, int y, int width, int height)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // 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, x, y, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            Bitmap img = Bitmap.FromHbitmap(hBitmap);

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return(img);
        }
예제 #8
0
파일: Program.cs 프로젝트: ebpo90/cobaAI
        //Get screenshot on the center of game
        public static System.Drawing.Image CaptureWindow(string name, bool followMouse)
        {
            if (Process.GetProcessesByName(name).Count() == 0)
            {
                giveErrorMessage($"Parece que você fechou o {name}...");
            }
            IntPtr handle = Process.GetProcessesByName(name)[0].MainWindowHandle;
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            screen_width  = windowRect.right - windowRect.left;
            screen_height = windowRect.bottom - windowRect.top;
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, size.X, size.Y);
            IntPtr hOld    = GDI32.SelectObject(hdcDest, hBitmap);

            if (followMouse)
            {
                GDI32.BitBlt(hdcDest, 0, 0, size.X, size.Y, hdcSrc, coordinates.X - size.X / 2, coordinates.Y - size.Y / 2, GDI32.SRCCOPY);
            }
            else
            {
                GDI32.BitBlt(hdcDest, 0, 0, size.X, size.Y, hdcSrc, screen_width / 2 - size.X / 2, screen_height / 2 - size.Y / 2, GDI32.SRCCOPY);
            }
            GDI32.SelectObject(hdcDest, hOld);
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            System.Drawing.Image img = System.Drawing.Image.FromHbitmap(hBitmap);
            GDI32.DeleteObject(hBitmap);
            return(img);
        }
예제 #9
0
        public static Bitmap GetDesktop()
        {
            int    screenX;
            int    screenY;
            IntPtr hBmp;
            IntPtr hdcScreen     = User32.GetDC(User32.GetDesktopWindow());
            IntPtr hdcCompatible = GDI32.CreateCompatibleDC(hdcScreen);

            screenX = User32.GetSystemMetrics(0);
            screenY = User32.GetSystemMetrics(1);
            hBmp    = GDI32.CreateCompatibleBitmap(hdcScreen, screenX, screenY);

            if (hBmp != IntPtr.Zero)
            {
                IntPtr hOldBmp = (IntPtr)GDI32.SelectObject(hdcCompatible, hBmp);
                GDI32.BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, GDI32.TernaryRasterOperations.SRCCOPY);

                GDI32.SelectObject(hdcCompatible, hOldBmp);
                GDI32.DeleteDC(hdcCompatible);
                User32.ReleaseDC(User32.GetDesktopWindow(), hdcScreen);

                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);

                GDI32.DeleteObject(hBmp);
                GC.Collect();

                return(bmp);
            }

            return(null);
        }
예제 #10
0
    static private Image CaptureWindow(IntPtr hWnd)
    {
        IntPtr hdcSrc = User32.GetWindowDC(hWnd);

        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(hWnd, 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);

        User32.PrintWindow(hWnd, hdcDest, 0);
        GDI32.SelectObject(hdcDest, hOld);

        GDI32.DeleteDC(hdcDest);
        User32.ReleaseDC(hWnd, hdcSrc);
        Image img = Image.FromHbitmap(hBitmap);

        GDI32.DeleteObject(hBitmap);

        return(img);
    }
예제 #11
0
        public override Bitmap PrintWindow()
        {
            //Rect rc;
            //// TODO: This might be stuck forever. Use timer instead?
            //int tries = 100;
            //while (!GetWindowRect(Screen, out rc) && tries-- > 0) { };

            //if (tries <= 0)
            //{
            //    throw new Exception("Failed to Print Window");
            //}

            //Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format24bppRgb);
            //Graphics gfxBmp = Graphics.FromImage(bmp);
            //IntPtr hdcBitmap = gfxBmp.GetHdc();

            //PrintWindow(Screen, hdcBitmap, 0);

            //gfxBmp.ReleaseHdc(hdcBitmap);
            //gfxBmp.Dispose();

            //bmp.Save("C:\\TestWin32\\test.png", ImageFormat.Png);

            //return bmp;

            // get te hDC of the target window
            IntPtr hdcSrc = GetWindowDC(Screen);
            // get the size
            Rect windowRect;

            GetWindowRect(Screen, 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);
            ReleaseDC(Screen, hdcSrc);

            // get a .NET image object for it
            Image img = Image.FromHbitmap(hBitmap);

            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);

            ((Bitmap)img).Save("C:\\TestWin32\\test.png", ImageFormat.Png);
            ((Bitmap)img).Save("C:\\TestWin32\\test2.jpeg", ImageFormat.Jpeg);

            return((Bitmap)img);
        }
예제 #12
0
        /// <summary>
        /// 截取指定句柄窗口的图像, 也可也截屏
        /// <para>当然 当前封装是依赖传入参数获取窗口大小,也可以使用API自动获取</para>
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static Bitmap CaptureWindow(IntPtr handle, int width, int height)
        {
            try
            {
                // get the hDC of the target window
                IntPtr hdcSrc = User32.GetWindowDC(handle);
                // 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
                Bitmap img = Image.FromHbitmap(hBitmap);
                // free up the Bitmap object
                GDI32.DeleteObject(hBitmap);

                return(img);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(null);
        }
예제 #13
0
        public System.Drawing.Image CaptureWindow(IntPtr handle, int picWidth)
        {
            try
            {
                //User32.SetForegroundWindow(handle);
                Rectangle   bRect  = new Rectangle();
                IntPtr      hdcSrc = User32.GetWindowDC(handle);
                User32.RECT wRect  = new User32.RECT();
                User32.GetWindowRect(handle, ref wRect);
                bRect.Width  = picWidth;
                bRect.Height = picWidth;
                bRect.X      = (wRect.right - wRect.left) / 2 - (picWidth / 2);
                bRect.Y      = (wRect.bottom - wRect.top) / 2 - (picWidth / 2);

                //bRect.Width = picWidth;
                //bRect.Height = picWidth;
                //bRect.X = 0;// (wRect.right - wRect.left) / 2 - (picWidth / 2);
                //bRect.Y = 0;// (wRect.bottom - wRect.top) / 2 - (picWidth / 2);

                IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
                IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, bRect.Width, bRect.Height);
                IntPtr hOld    = GDI32.SelectObject(hdcDest, hBitmap);
                GDI32.BitBlt(hdcDest, 0, 0, bRect.Width, bRect.Height, hdcSrc, bRect.Left, bRect.Top, GDI32.SRCCOPY);
                GDI32.SelectObject(hdcDest, hOld);
                System.Drawing.Image img = System.Drawing.Image.FromHbitmap(hBitmap);
                GDI32.DeleteDC(hdcDest);
                User32.ReleaseDC(handle, hdcSrc);
                GDI32.DeleteObject(hBitmap);
                return(img);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
예제 #14
0
 private static void Cleanup(int hBitmap, int hdcSrc, int hdcDest)
 {
     GDI32.SelectObject(hdcDest, hBitmap);
     User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc);
     GDI32.DeleteDC(hdcDest);
     GDI32.DeleteObject(hBitmap);
 }
예제 #15
0
            /// <summary>
            /// Creates an Image object containing a screen shot of a specific window
            /// </summary>
            /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
            /// <returns></returns>
            public Image CaptureWindow(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);
                // get a .NET image object for it
                Image img = Image.FromHbitmap(hBitmap);

                // free up the Bitmap object
                GDI32.DeleteObject(hBitmap);
                return(img);
            }
        /*public static Image CaptureWindow(IntPtr handle)
         * {
         *  User32.RECT windowRect = new User32.RECT();
         *  User32.GetWindowRect(handle,ref windowRect);
         *  Rectangle r = new Rectangle(
         *      0,
         *      0,
         *      windowRect.right - windowRect.left,
         *      windowRect.bottom - windowRect.top
         *  );
         *  return CaptureWindow(handle, r);
         * }*/

        public static Image CaptureWindow(IntPtr handle, Rectangle rect)
        {
            // get te hDC of the target window
            IntPtr hdcSrc = User32.GetWindowDC(handle);
            // get the size
            int left   = rect.X;
            int top    = rect.Y;
            int width  = rect.Width;
            int height = rect.Height;
            // 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, left, top, 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);
        }
예제 #17
0
        private Image CaptureWindow(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 borderSize   = Math.Abs(this.Width - this.ClientRectangle.Width) / 2;
            int titleBarSize = Math.Abs(this.Height - borderSize - this.ClientRectangle.Height);
            int width        = windowRect.right - windowRect.left - 2 * borderSize;
            int height       = windowRect.bottom - windowRect.top - titleBarSize - borderSize;
            // 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);
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, borderSize, titleBarSize, 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);
        }
예제 #18
0
        public Image 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 img = Image.FromHbitmap(hBitmap);

            GDI32.DeleteObject(hBitmap);

            return(img);
        }
예제 #19
0
        /// <summary>
        /// Create un objeto imagen que contiene una captura de pantalla de la ventana actual
        /// </summary>
        /// <param name="handle">La instancia Handle de la ventana (En windows forms, es obtenida a traves de la propiedad Handle)</param>
        /// <returns></returns>
        public Image CaptureWindow(IntPtr handle)
        {
            // obtener hDC de la ventana deseada
            IntPtr hdcSrc = User32.GetWindowDC(handle);

            // Obtener el tamano
            User32.RECT windowRect = new User32.RECT();
            User32.GetWindowRect(handle, ref windowRect);
            int width  = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            // Crea un contexto en el que se copiara la imagen
            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);
            // Seleccionar el objeto bitmap
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);

            // finalizar bitblt
            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
            // Restaurar seleccion
            GDI32.SelectObject(hdcDest, hOld);
            // Limpiar
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // Obtener una imagen .NET image del bitmap
            Image img = Image.FromHbitmap(hBitmap);

            // Liberar objeto Bitmab
            GDI32.DeleteObject(hBitmap);
            return(img);
        }
예제 #20
0
        public static Image Capture()
        {
            IntPtr windowDc = User32.GetWindowDC(handle);

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

            int nWidth  = rect.right - rect.left;
            int nHeight = rect.bottom - rect.top;

            IntPtr compatibleDc     = GDI32.CreateCompatibleDC(windowDc);
            IntPtr compatibleBitmap = GDI32.CreateCompatibleBitmap(windowDc, nWidth, nHeight);
            IntPtr hObject          = GDI32.SelectObject(compatibleDc, compatibleBitmap);

            GDI32.BitBlt(compatibleDc, 0, 0, nWidth, nHeight, windowDc, 0, 0, GDI32.SRCCOPY);
            GDI32.SelectObject(compatibleDc, hObject);

            Image image = Image.FromHbitmap(compatibleBitmap);

            GDI32.DeleteObject(compatibleBitmap);
            GDI32.DeleteDC(compatibleDc);
            User32.ReleaseDC(handle, windowDc);

            return(image);
        }
예제 #21
0
            /// <summary>
            /// Captures the window or part thereof to a bitmap image.
            /// </summary>
            /// <param name="wndHWND">window handle</param>
            /// <param name="x">x location in window</param>
            /// <param name="y">y location in window</param>
            /// <param name="width">width of capture area</param>
            /// <param name="height">height of capture area</param>
            /// <returns>window bitmap</returns>
            public static Bitmap Window(IntPtr wndHWND, int x, int y, int width, int height)
            {
                IntPtr wndHDC = USER32.GetDC(wndHWND);          // get context for window

                //	create compatibile capture context and bitmap
                IntPtr capHDC = GDI32.CreateCompatibleDC(wndHDC);
                IntPtr capBMP = GDI32.CreateCompatibleBitmap(wndHDC, width, height);

                //	make sure bitmap non-zero
                if (capBMP == IntPtr.Zero)                      // if no compatible bitmap
                {
                    USER32.ReleaseDC(wndHWND, wndHDC);          //   release window context
                    GDI32.DeleteDC(capHDC);                     //   delete capture context
                    return(null);                               //   return null bitmap
                }

                //	select compatible bitmap in compatible context
                //	copy window context to compatible context
                //  select previous bitmap back into compatible context
                IntPtr prvHDC = (IntPtr)GDI32.SelectObject(capHDC, capBMP);

                GDI32.BitBlt(capHDC, 0, 0, width, height, wndHDC, x, y, GDI32.SRCCOPY);
                GDI32.SelectObject(capHDC, prvHDC);

                //	create GDI+ bitmap for window
                Bitmap bmp = System.Drawing.Image.FromHbitmap(capBMP);

                //	release window and capture resources
                USER32.ReleaseDC(wndHWND, wndHDC);                      // release window context
                GDI32.DeleteDC(capHDC);                                 // delete capture context
                GDI32.DeleteObject(capBMP);                             // delete capture bitmap

                //	return bitmap image to user
                return(bmp);                                                                     // return bitmap
            }
예제 #22
0
        private static System.Drawing.Bitmap CaptureWindow(IntPtr handle, Rectangle region)
        {
            // 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  = region.Width;
            int height = region.Height;

            // 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, region.X, region.Y, GDI32.SRCCOPY);
            // restore selection
            GDI32.SelectObject(hdcDest, hOld);
            // clean up
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            // get a .NET image object for it
            System.Drawing.Bitmap img = System.Drawing.Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return(img);
        }
예제 #23
0
        public static Bitmap CopyFromScreen(IntPtr bdoHandle)
        {
            User32.SIZE size;
            var         hDc    = User32.GetDC(bdoHandle);
            var         hMemDc = GDI32.CreateCompatibleDC(hDc);

            size.cx = User32.GetSystemMetrics(User32.SM_CXSCREEN);
            size.cy = User32.GetSystemMetrics(User32.SM_CYSCREEN);

            var hBitmap = GDI32.CreateCompatibleBitmap(hDc, size.cx, size.cy);

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

            var hOld = GDI32.SelectObject(hMemDc, hBitmap);

            GDI32.BitBlt(hMemDc, 0, 0, size.cx, size.cy, hDc, 0, 0, GDI32.SRCCOPY);
            GDI32.SelectObject(hMemDc, hOld);
            GDI32.DeleteDC(hMemDc);
            User32.ReleaseDC(bdoHandle, hDc);
            var bmp = Image.FromHbitmap(hBitmap);

            GDI32.DeleteObject(hBitmap);
            GC.Collect();
            return(bmp);
        }
예제 #24
0
        public static Image CaptureWindow(IntPtr handle)
        {
            int width, height;

            {
                User32.RECT windowRect;
                User32.GetWindowRect(handle, out windowRect);
                width  = windowRect.right - windowRect.left;
                height = windowRect.bottom - windowRect.top;
            }
            Debug.Assert(width > 0 && height > 0);
            Image img = null;

            {
                IntPtr hdcSrc  = User32.GetWindowDC(handle);
                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.SRCDOPY | GDI32.CAPTUREBLT);
                    GDI32.SelectObject(hdcDest, hOld);
                    img = Image.FromHbitmap(hBitmap);
                }
                GDI32.DeleteObject(hBitmap);
                GDI32.DeleteDC(hdcDest);
                User32.ReleaseDC(handle, hdcSrc);
            }
            return(img);
        }
예제 #25
0
    private Image PrintScreen(IntPtr hWnd)
    {
        //https://blog.csdn.net/spiderlily/article/details/8548470
        // User32.dll PrintWindow
        IntPtr hscrdc = User32.GetWindowDC(hWnd);

        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(hWnd, ref windowRect);
        int    width   = windowRect.right - windowRect.left;
        int    height  = windowRect.bottom - windowRect.top;
        IntPtr hbitmap = GDI32.CreateCompatibleBitmap(hscrdc, width, height);
        IntPtr hmemdc  = GDI32.CreateCompatibleDC(hscrdc);

        GDI32.SelectObject(hmemdc, hbitmap);
        bool   re  = User32.PrintWindow(hWnd, hmemdc, 0);
        Bitmap bmp = null;

        if (re)
        {
            bmp = Bitmap.FromHbitmap(hbitmap);
        }
        GDI32.DeleteObject(hbitmap);
        GDI32.DeleteDC(hmemdc);
        User32.ReleaseDC(hWnd, hscrdc);
        return(bmp);
    }
예제 #26
0
        //dirtyRect是绝对坐标(在多屏的情况下)
        public virtual void SetHBitmap(IntPtr hBitmap, Rectangle newWindowBounds, Point drawAt, Rectangle dirtyRect, byte opacity)
        {
            // IntPtr screenDc = Win32.GDI32.GetDC(IntPtr.Zero);

            IntPtr memDc     = Win32.GDI32.CreateCompatibleDC(IntPtr.Zero);
            IntPtr oldBitmap = IntPtr.Zero;

            try
            {
                oldBitmap = Win32.GDI32.SelectObject(memDc, hBitmap);

                var winSize = new Native.Size(newWindowBounds.Width, newWindowBounds.Height);
                var winPos  = new Native.Point(newWindowBounds.X, newWindowBounds.Y);

                var drawBmpAt = new Native.Point(drawAt.X, drawAt.Y);
                var blend     = new Native.BLENDFUNCTION {
                    BlendOp = GDI32.AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = opacity, AlphaFormat = GDI32.AC_SRC_ALPHA
                };

                var updateInfo = new Native.UPDATELAYEREDWINDOWINFO();
                updateInfo.cbSize  = (uint)Marshal.SizeOf(typeof(Native.UPDATELAYEREDWINDOWINFO));
                updateInfo.dwFlags = GDI32.ULW_ALPHA;
                updateInfo.hdcDst  = IntPtr.Zero;//Native.GetDC(IntPtr.Zero);//IntPtr.Zero; //ScreenDC
                updateInfo.hdcSrc  = memDc;

                // dirtyRect.X -= _bounds.X;
                // dirtyRect.Y -= _bounds.Y;

                //dirtyRect.Offset(-_bounds.X, -_bounds.Y);
                var dirRect = new GDI32.RECT(dirtyRect.X, dirtyRect.Y, dirtyRect.Right, dirtyRect.Bottom);

                unsafe
                {
                    updateInfo.pblend   = &blend;
                    updateInfo.pptDst   = &winPos;
                    updateInfo.psize    = &winSize;
                    updateInfo.pptSrc   = &drawBmpAt;
                    updateInfo.prcDirty = &dirRect;
                }

                Native.UpdateLayeredWindowIndirect(Handle, ref updateInfo);
                // Debug.Assert(Native.GetLastError() == 0);

                //Native.UpdateLayeredWindow(Handle, IntPtr.Zero, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, GDI32.ULW_ALPHA);
            }
            finally
            {
                //GDI32.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 Win32 GDI and it's working fine without any resource leak.
                    //Win32.DeleteObject(hBitmap);
                }
                GDI32.DeleteDC(memDc);
            }
        }
예제 #27
0
        /// <summary>
        ///  Free resources
        /// </summary>
        private void Cleanup()
        {
            if (hOldObject != IntPtr.Zero && hDCDest != IntPtr.Zero)
            {
                // restore selection (old handle)
                GDI32.SelectObject(hDCDest, hOldObject);
                GDI32.DeleteDC(hDCDest);
            }
            if (hDCDesktop != IntPtr.Zero)
            {
                User32.ReleaseDC(hWndDesktop, hDCDesktop);
            }
            if (hDIBSection != IntPtr.Zero)
            {
                // free up the Bitmap object
                GDI32.DeleteObject(hDIBSection);
            }

            if (disabledDWM)
            {
                DWM.EnableComposition();
            }
            if (aviWriter != null)
            {
                aviWriter.Dispose();
                aviWriter = null;

                string ffmpegexe = PluginUtils.GetExePath("ffmpeg.exe");
                if (ffmpegexe != null)
                {
                    try {
                        string webMFile  = filename.Replace(".avi", ".webm");
                        string arguments = "-i \"" + filename + "\" -codec:v libvpx -quality good -cpu-used 0 -b:v 1000k -qmin 10 -qmax 42 -maxrate 1000k -bufsize 4000k -threads 4 \"" + webMFile + "\"";
                        LOG.DebugFormat("Starting {0} with arguments {1}", ffmpegexe, arguments);
                        ProcessStartInfo processStartInfo = new ProcessStartInfo(ffmpegexe, arguments);
                        processStartInfo.CreateNoWindow         = false;
                        processStartInfo.RedirectStandardOutput = false;
                        processStartInfo.UseShellExecute        = false;
                        Process process = Process.Start(processStartInfo);
                        process.WaitForExit();
                        if (process.ExitCode == 0)
                        {
                            MessageBox.Show("Recording written to " + webMFile);
                        }
                    } catch (Exception ex) {
                        MessageBox.Show("Recording written to " + filename + " couldn't convert due to an error: " + ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("Recording written to " + filename);
                }
            }
        }
예제 #28
0
        /// <summary>
        /// Creates an Image object containing a screen shot of a specific window
        /// </summary>
        /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
        /// <returns></returns>
        private static void CaptureWindow(IntPtr handle)
        {
            // memoryStream.Position = 0;
            // 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);
            width  = windowRect.right - windowRect.left;
            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
            Bitmap     bmp     = Bitmap.FromHbitmap(hBitmap);
            Rectangle  rect    = new Rectangle(0, 0, width, height);
            BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);

            unsafe
            {
                byte *p = (byte *)bmpData.Scan0.ToPointer();
                for (int y = 0, k = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        byte r = p[2];
                        byte g = p[1];
                        byte b = p[0];
                        p += 4;
                        screenBitmap[k++] = (UInt32)(((r << 16) | (g << 8) | b) | 0xff000000);
                    }
                    p += bmpData.Stride - width * 4;
                }
            }

            // free up the Bitmap object
            bmp.UnlockBits(bmpData);
            bmp.Dispose();
            GDI32.DeleteObject(hBitmap);
        }
예제 #29
0
 void IDisposable.Dispose()
 {
     this.hdcSrc = null;
     try
     {
         // clean up
         GDI32.DeleteDC(this.dc);
         this.dc = new IntPtr();
     }
     catch (Exception)
     { }
 }
예제 #30
0
        public static Bitmap CaptureRectangle(Rectangle captureBounds)
        {
            IntPtr hDesktop   = User32.GetDesktopWindow();
            IntPtr hDC        = User32.GetWindowDC(hDesktop);
            IntPtr hDest      = GDI32.CreateCompatibleDC(hDC);
            IntPtr hBitmap    = GDI32.CreateCompatibleBitmap(hDC, captureBounds.Width, captureBounds.Height);
            IntPtr hOldBitmap = GDI32.SelectObject(hDest, hBitmap);

            GDI32.BitBlt(hDest, 0, 0, captureBounds.Width, captureBounds.Height,
                         hDC, captureBounds.X, captureBounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

            Bitmap bitmap        = null;
            bool   isRegionEmpty = true;

            using (var graphics = Graphics.FromHwnd(hDesktop))
            {
                isRegionEmpty = WindowCapture.IsRegionEmpty(graphics, captureBounds);
            }

            if (isRegionEmpty)
            {
                bitmap = Bitmap.FromHbitmap(hBitmap);
            }
            else
            {
                float xDpi = 96F, yDpi = 96F;

                using (var tmp = Bitmap.FromHbitmap(hBitmap))
                {
                    xDpi = tmp.HorizontalResolution;
                    yDpi = tmp.VerticalResolution;
                }

                bitmap = WindowCapture.CreateEmpty(captureBounds.Width, captureBounds.Height, PixelFormat.Format32bppArgb, Color.Transparent, xDpi, yDpi);

                using (var graphics = Graphics.FromImage(bitmap))
                {
                    foreach (var screen in Screen.AllScreens)
                    {
                        var bounds = screen.Bounds;
                        bounds.Offset(-captureBounds.X, -captureBounds.Y);
                        graphics.DrawImage(bitmap, bounds, bounds.X, bounds.Y, bounds.Width, bounds.Height, GraphicsUnit.Pixel);
                    }
                }
            }

            GDI32.SelectObject(hDest, hOldBitmap);
            GDI32.DeleteObject(hBitmap);
            GDI32.DeleteDC(hDest);
            User32.ReleaseDC(hDesktop, hDC);

            return(bitmap);
        }