Ejemplo n.º 1
0
        public System.Drawing.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);

            System.Drawing.Image image = System.Drawing.Image.FromHbitmap(hBitmap);

            GDI32.DeleteObject(hBitmap);

            return(image);
        }
Ejemplo n.º 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);
  }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        //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);
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
    }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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);
            }
        }
Ejemplo n.º 16
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);
                }
            }
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
        public Pix CaptureWindowPix(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);

            int bitsPerPixel  = ((int)PixelFormat.Format32bppArgb & 0xff00) >> 8;
            int bytesPerPixel = (bitsPerPixel + 7) / 8;
            int stride        = 4 * ((width * bytesPerPixel + 3) / 4);

            //Bitmap intermediate = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, hBitmap);
            //Pix img = PixConverter.ToPix(intermediate);

            Bitmap orig  = Image.FromHbitmap(hBitmap);
            Bitmap clone = new Bitmap(orig.Width, orig.Height, PixelFormat.Format32bppArgb);

            using (Graphics gr = Graphics.FromImage(clone))
            {
                gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
            }
            orig.Dispose();

            Pix img = PixConverter.ToPix(clone);

            //Pix img = PixConverter.ToPix(Image.FromHbitmap(hBitmap));
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return(img);
        }
Ejemplo n.º 20
0
        public Image CaptureGameScreen(IntPtr handle)
        {
            int    windowTopOffset = 55;
            IntPtr hdcSrc          = User32.GetWindowDC(handle);

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

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

            int diffx   = 0;
            int offsetx = 0;

            int diffy   = 0;
            int offsety = 0;

            if (height / width < 0.9375)
            {
                var idealWidth = (int)(height * 1.0667);
                diffx   = width - idealWidth > 0 ? width - idealWidth : 0;
                offsetx = diffx / 2;
            }
            else if (height / width > 0.9375)
            {
                var idealHeight = (int)(width * 0.9375);
                diffy   = height - idealHeight > 0 ? height - idealHeight : 0;
                offsety = diffy / 2;
            }

            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width - diffx, height - diffy);
            IntPtr hOld    = GDI32.SelectObject(hdcDest, hBitmap);

            GDI32.BitBlt(hdcDest, 0, 0, width - offsetx, height - offsety, hdcSrc, 10 + offsetx, windowTopOffset + offsety, GDI32.SRCCOPY);

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

            Debug.Print(img.HorizontalResolution.ToString());
            Debug.Print(img.VerticalResolution.ToString());
            Debug.Print(img.Width.ToString());
            Debug.Print(img.Height.ToString());
            GDI32.DeleteObject(hBitmap);

            return(ResizeImage(img, 256, 240));
        }
Ejemplo n.º 21
0
        //-------------------------------------------------------------------

        /// 現在の対象ウィンドウのClient領域をXORで塗りつぶす
        private void XorTargetWindowRect()
        {
            User32.RECT currentTargetRect;
            User32.GetClientRect(this.currentTargetWindow, out currentTargetRect);

            var originalDrawMode = GDI32.SetROP2(this.currentTargetDC, GDI32.R2_XORPEN);
            var originalPen      = GDI32.SelectObject(this.currentTargetDC, App.NullPen.Pen);

            GDI32.Rectangle(this.currentTargetDC,
                            currentTargetRect.Left, currentTargetRect.Top,
                            currentTargetRect.Right, currentTargetRect.Bottom);

            GDI32.SelectObject(this.currentTargetDC, originalPen);
            GDI32.SetROP2(this.currentTargetDC, originalDrawMode);
        }
Ejemplo n.º 22
0
        public static Bitmap PrintWindow(IntPtr hWnd)
        {
            IntPtr  hscrdc  = User32.GetWindowDC(hWnd);
            Control control = Control.FromHandle(hWnd);
            IntPtr  hbitmap = GDI32.CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
            IntPtr  hmemdc  = GDI32.CreateCompatibleDC(hscrdc);

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

            GDI32.DeleteDC(hscrdc);            //删除用过的对象
            GDI32.DeleteDC(hmemdc);            //删除用过的对象
            return(bmp);
        }
Ejemplo n.º 23
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 (Bitmap Image, Rectangle WindowRect) CaptureWindow(IntPtr handle, Rectangle?rect = null)
        {
            // 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;
            var windowRect2 = new Rectangle(windowRect.left, windowRect.top, width, height);

            Rectangle captureArea;

            if (rect.HasValue)
            {
                captureArea = rect.Value;
            }
            else
            {
                captureArea = new Rectangle(0, 0, width, 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, captureArea.Width, captureArea.Height);
            // select the bitmap object
            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);



            // bitblt over
            GDI32.BitBlt(hdcDest, 0, 0, captureArea.Width, captureArea.Height, hdcSrc, captureArea.X, captureArea.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);

            //Image img = Image.FromHbitmap(hBitmap);
            // free up the Bitmap object
            GDI32.DeleteObject(hBitmap);
            return(img, windowRect2);
        }
Ejemplo n.º 24
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, Point tl, Point br)
        {
            IntPtr hdcSrc  = User32.GetWindowDC(handle);
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, br.X - tl.X, br.Y - tl.Y);
            IntPtr hOld    = GDI32.SelectObject(hdcDest, hBitmap);

            GDI32.BitBlt(hdcDest, 0, 0, br.X - tl.X, br.Y - tl.Y, hdcSrc, tl.X, tl.Y, GDI32.SRCCOPY);
            GDI32.SelectObject(hdcDest, hOld);
            GDI32.DeleteDC(hdcDest);
            User32.ReleaseDC(handle, hdcSrc);
            Image img = Image.FromHbitmap(hBitmap);

            GDI32.DeleteObject(hBitmap);
            return(img);
        }
Ejemplo n.º 25
0
        public override Bitmap CaptureScreenArea(int X, int Y, int Width, int Height)
        {
            IntPtr hdcSrc  = GDI32.CreateDC("DISPLAY", null, null, IntPtr.Zero);
            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, Width, Height);

            GDI32.SelectObject(hdcDest, hBitmap);
            GDI32.BitBlt(hdcDest, 0, 0, Width, Height, hdcSrc, User32.ScreenOffsetX + X, User32.ScreenOffsetY + Y,
                         0x40000000 | 0x00CC0020); //SRCCOPY AND CAPTUREBLT
            Bitmap bmpret = Bitmap.FromHbitmap(hBitmap);

            GDI32.DeleteDC(hdcSrc);
            GDI32.DeleteDC(hdcDest);
            GDI32.DeleteObject(hBitmap);
            return(bmpret);
        }
Ejemplo n.º 26
0
        private void CaptureFrame(IntPtr handle, CancellationToken token)
        {
            // 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);
            IntPtr hBitmap;
            IntPtr hOld;

            // create a bitmap we can copy it to,
            // using GetDeviceCaps to get the width/height
            hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
            // select the bitmap object
            hOld = GDI32.SelectObject(hdcDest, hBitmap);

            try
            {
                while (true)
                {
                    token.ThrowIfCancellationRequested();
                    // select the bitmap object
                    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);
                    // get a .NET image object for it
                    screen = Image.FromHbitmap(hBitmap);
                    GC.Collect();
                }
            }
            catch (TaskCanceledException ex)
            {
                // clean up
                // free up the Bitmap object
                GDI32.DeleteObject(hBitmap);
                GDI32.DeleteDC(hdcDest);
                User32.ReleaseDC(handle, hdcSrc);
                Logger.Log(ex);
            }
        }
Ejemplo n.º 27
0
        public Image CaptureWindowSBS(int resizewidth, int resizeheight)
        {
            // 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);

            if (resizewidth > img.Width || resizeheight > img.Height)
            {
                Image    newImage   = new Bitmap(resizewidth, resizeheight);
                Graphics g          = Graphics.FromImage(newImage);
                int      resizeleft = (resizewidth - img.Width) / 2;
                int      resizetop  = (resizeheight - img.Height) / 2;
                //g.DrawImage(img, resizeleft, resizetop, img.Width / 2, img.Height);
                g.DrawImage(img, 0, resizetop, new Rectangle(0, 0, img.Width / 2, img.Height), GraphicsUnit.Pixel);
                g.DrawImage(img, 0 + resizewidth / 2, resizetop, new Rectangle(img.Width / 2, 0, img.Width / 2, img.Height), GraphicsUnit.Pixel);
                img.Dispose();
                img = null;

                return(newImage);
            }
            else
            {
                return(img);
            }
        }
Ejemplo n.º 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 Bitmap CaptureWindow(IntPtr handle, double dpi)
        {
            // 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  = (int)((windowRect.right - windowRect.left) * dpi);
            int height = (int)((windowRect.bottom - windowRect.top) * dpi);
            // 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
            bool success = GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);

            try
            {
                if (success)
                {
                    Bitmap bitmap = (Bitmap)Image.FromHbitmap(hBitmap);
                    bitmap.MakeTransparent(Color.Transparent);

                    return(bitmap);
                }
                else
                {
                    return(null);
                }
            }
            finally
            {
                // restore selection
                GDI32.SelectObject(hdcDest, hOld);
                // clean up
                GDI32.DeleteDC(hdcDest);
                User32.ReleaseDC(handle, hdcSrc);
                // free up the Bitmap object
                GDI32.DeleteObject(hBitmap);
            }
        }
Ejemplo n.º 29
0
        public static Bitmap GetWindowCapture(IntPtr hWnd)
        {
            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);
            User32.PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);

            GDI32.DeleteDC(hscrdc); //删除用过的对象
            GDI32.DeleteDC(hmemdc); //删除用过的对象
            return(bmp);
        }
Ejemplo n.º 30
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)
    {
        int TotalWidth = 0, TotalHeight = 0;

        for (int i = 0; i < Screen.AllScreens.Length; i++)
        {
            Screen src = Screen.AllScreens[i];
            TotalWidth  += src.Bounds.Width;
            TotalHeight += src.Bounds.Height;
        }

        IntPtr hdcSrc = User32.GetWindowDC(handle);

        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(handle, ref windowRect);
        int width  = TotalWidth - Screen.PrimaryScreen.Bounds.Left;
        int height = TotalHeight - Screen.PrimaryScreen.Bounds.Top;
        //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);
    }