Exemplo n.º 1
0
        public static Rectangle GetWindowRect(this IntPtr hWnd)
        {
            var rect = new User32Api.RectApi();

            User32Api.GetWindowRect(hWnd, ref rect);
            return(rect.ToRectangle());
        }
Exemplo n.º 2
0
        public static Bitmap Capture(this IntPtr hWnd)
        {
            var hscrdc     = User32Api.GetWindowDC(hWnd);
            var windowRect = new User32Api.RectApi();

            User32Api.GetWindowRect(hWnd, ref windowRect);
            var width  = windowRect.right - windowRect.left;
            var height = windowRect.bottom - windowRect.top;

            var hbitmap = Gdi32Api.CreateCompatibleBitmap(hscrdc, width, height);
            var hmemdc  = Gdi32Api.CreateCompatibleDC(hscrdc);

            Gdi32Api.SelectObject(hmemdc, hbitmap);
            User32Api.PrintWindow(hWnd, hmemdc, 0);
            var bmp = Image.FromHbitmap(hbitmap);

            Gdi32Api.DeleteDC(hscrdc); //删除用过的对象
            Gdi32Api.DeleteDC(hmemdc); //删除用过的对象

            GC.Collect();

            return(bmp);
        }