Esempio n. 1
0
        /// <summary>
        /// アクティブなウィンドウの画像を取得する
        /// </summary>
        /// <returns>アクティブなウィンドウの画像</returns>
        public static Bitmap CaptureActiveWindow()
        {
            //アクティブなウィンドウのデバイスコンテキストを取得
            IntPtr hWnd   = WindowAPIWrapper.GetForegroundWindowHandle();
            IntPtr hWinDC = NativeMethod.GetWindowDC(hWnd);

            //ウィンドウの大きさを取得
            var winRect = new Rect();

            NativeMethod.GetWindowRect(hWnd, ref winRect);

            //Bitmapの作成
            var bmp = new Bitmap(winRect.Right - winRect.Left,
                                 winRect.Bottom - winRect.Top);

            //Graphicsの作成
            using (var g = Graphics.FromImage(bmp))
            {
                //Graphicsのデバイスコンテキストを取得
                IntPtr hDC = g.GetHdc();

                //Bitmapに画像をコピーする
                NativeMethod.BitBlt(hDC, 0, 0, bmp.Width, bmp.Height,
                                    hWinDC, 0, 0, NativeMethod.SRCCOPY);

                //解放
                g.ReleaseHdc(hDC);
            }
            NativeMethod.ReleaseDC(hWnd, hWinDC);

            return(bmp);
        }
Esempio n. 2
0
        public void Draw(Graphics g, Rectangle rect)
        {
            IntPtr outputHDC = g.GetHdc();

            NativeMethod.BitBlt(outputHDC, rect.X, rect.Y, rect.Width, rect.Height, this._TextHDC, 0, 0, BitBltOp.SRCCOPY);
            g.ReleaseHdc(outputHDC);
        }
Esempio n. 3
0
        /// <summary>
        /// プライマリスクリーンの画像を取得する
        /// </summary>
        /// <returns>プライマリスクリーンの画像</returns>
        public static Bitmap CaptureScreen()
        {
            //プライマリモニタのデバイスコンテキストを取得
            IntPtr disDC = NativeMethod.GetDC(IntPtr.Zero);

            //Bitmapの作成
            var bmp = new Bitmap(
                Screen.PrimaryScreen.Bounds.Width,
                Screen.PrimaryScreen.Bounds.Height);

            //Graphicsの作成
            using (var g = Graphics.FromImage(bmp))
            {
                //Graphicsのデバイスコンテキストを取得
                IntPtr hDC = g.GetHdc();

                //Bitmapに画像をコピーする
                NativeMethod.BitBlt(hDC, 0, 0, bmp.Width, bmp.Height,
                                    disDC, 0, 0, NativeMethod.SRCCOPY);
                //解放
                g.ReleaseHdc(hDC);
            }
            NativeMethod.ReleaseDC(IntPtr.Zero, disDC); //32bit

            return(bmp);
        }
Esempio n. 4
0
        /// <summary>アクティブになっているウィドウをキャプチャします</summary>
        public static Bitmap CaptureActiveWindow(HWND hWnd)
        {
            //アクティブなウィンドウのデバイスコンテキストを取得
            IntPtr winDC = NativeMethod.GetWindowDC(hWnd);
            //ウィンドウの大きさを取得
            RECT winRect = new RECT();

            NativeMethod.GetWindowRect(hWnd, ref winRect);
            //Bitmapの作成
            Bitmap bmp = new Bitmap(winRect.right - winRect.left,
                                    winRect.bottom - winRect.top);
            //Graphicsの作成
            Graphics g = Graphics.FromImage(bmp);
            //Graphicsのデバイスコンテキストを取得
            IntPtr hDC = g.GetHdc();

            //Bitmapに画像をコピーする
            NativeMethod.BitBlt(hDC, 0, 0, bmp.Width, bmp.Height,
                                winDC, 0, 0, BitBltOp.SRCCOPY);
            //解放
            g.ReleaseHdc(hDC);
            g.Dispose();
            NativeMethod.ReleaseDC(hWnd, winDC);

            return(bmp);
        }
Esempio n. 5
0
        /// <summary>プライマリスクリーンをキャプチャします</summary>
        public static Bitmap CaptureScreen(Screen scr)
        {
            //プライマリモニタのデバイスコンテキストを取得
            IntPtr disDC = NativeMethod.GetDC(IntPtr.Zero);
            //Bitmapの作成
            Bitmap bmp = new Bitmap(scr.Bounds.Width,
                                    scr.Bounds.Height);
            //Graphicsの作成
            Graphics g = Graphics.FromImage(bmp);
            //Graphicsのデバイスコンテキストを取得
            IntPtr hDC = g.GetHdc();

            //Bitmapに画像をコピーする
            NativeMethod.BitBlt(hDC, 0, 0, bmp.Width, bmp.Height,
                                disDC, scr.Bounds.X, scr.Bounds.Y, BitBltOp.SRCCOPY);
            //解放
            g.ReleaseHdc(hDC);
            g.Dispose();
            NativeMethod.ReleaseDC(IntPtr.Zero, disDC);

            return(bmp);
        }
Esempio n. 6
0
        /// <summary>
        /// Windows10のドロップシャドウ対策入りのキャプチャ
        /// </summary>
        /// <returns></returns>
        public static Bitmap CaptureActiveWindow10()
        {
            //アクティブなウィンドウのデバイスコンテキストを取得
            IntPtr hWnd   = WindowAPIWrapper.GetForegroundWindowHandle();
            IntPtr hWinDC = NativeMethod.GetWindowDC(hWnd);

            //ウィンドウの大きさを取得
            var  winRect = new Rect();
            Rect bounds;

            NativeMethod.DwmGetWindowAttribute(hWnd,
                                               DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS,
                                               out bounds, Marshal.SizeOf(typeof(Rect)));

            NativeMethod.GetWindowRect(hWnd, ref winRect);
            //Bitmapの作成
            var offsetX = bounds.Left - winRect.Left;
            var offsetY = bounds.Top - winRect.Top;
            var bmp     = new Bitmap(bounds.Right - bounds.Left,
                                     bounds.Bottom - bounds.Top);

            //Graphicsの作成
            using (var g = Graphics.FromImage(bmp))
            {
                //Graphicsのデバイスコンテキストを取得
                IntPtr hDC = g.GetHdc();
                //Bitmapに画像をコピーする
                Int32 bmpWidth  = bmp.Width;
                Int32 bmpHeight = bmp.Height;
                Console.WriteLine(winRect);
                NativeMethod.BitBlt(hDC, 0, 0, bmpWidth, bmpHeight,
                                    hWinDC, offsetX, offsetY, NativeMethod.SRCCOPY);
                //解放
                g.ReleaseHdc(hDC);
            }
            NativeMethod.ReleaseDC(hWnd, hWinDC);
            return(bmp);
        }
Esempio n. 7
0
        public static byte[] CaptureRectangle(Rectangle r)
        {
            IntPtr wndHWND, wndHDC, capHDC, capBMP, prvHDC;

            wndHWND = wndHDC = capHDC = capBMP = prvHDC = IntPtr.Zero;
            byte[] buffer    = null;
            Point  cursorPos = Point.Empty;

            try
            {
                wndHWND = NativeMethod.GetDesktopWindow();      // window handle for desktop

                int x, y, width, height;
                x      = r.X;
                y      = r.Y;
                width  = r.Width;
                height = r.Height;

                wndHDC = NativeMethod.GetDC(wndHWND);           // get context for window

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

                //	make sure bitmap non-zero
                if (capBMP == IntPtr.Zero)                              // if no compatible bitmap
                {
                    NativeMethod.ReleaseDC(wndHWND, wndHDC);            //   release window context
                    NativeMethod.DeleteDC(capHDC);                      //   delete capture context
                    throw new Exception("Not create compatible bitmap.");
                }

                //	select compatible bitmap in compatible context
                //	copy window context to compatible context
                //  select previous bitmap back into compatible context
                prvHDC = (IntPtr)NativeMethod.SelectObject(capHDC, capBMP);
                //NativeMethod.BitBlt(capHDC, 0, 0, width, height, wndHDC, x, y, RasterOp.SRCCOPY | RasterOp.CAPTUREBLT);
                NativeMethod.BitBlt(capHDC, 0, 0, width, height, wndHDC, x, y, RasterOp.SRCCOPY);

                NativeMethod.GetCursorPos(out cursorPos);
                cursorPos.X -= r.Left;
                cursorPos.Y -= r.Top;

                // Draw the cursor
                // Always wait cursor, why? So use arrow cursor forever.
                //IntPtr hcur = GetCursor();
                IntPtr hcur = NativeMethod.GetCurrentCursorHandle();

                IconInfo iconInfo = new IconInfo();
                int      hr       = NativeMethod.GetIconInfo(hcur, out iconInfo);
                if (hr != 0)
                {
                    cursorPos.X -= iconInfo.xHotspot;
                    cursorPos.Y -= iconInfo.yHotspot;
                    if (iconInfo.hbmMask != IntPtr.Zero)
                    {
                        NativeMethod.DeleteObject(iconInfo.hbmMask);
                    }
                    if (iconInfo.hbmColor != IntPtr.Zero)
                    {
                        NativeMethod.DeleteObject(iconInfo.hbmColor);
                    }
                }
                NativeMethod.DrawIcon(capHDC, cursorPos.X, cursorPos.Y, hcur);

                NativeMethod.SelectObject(capHDC, prvHDC);

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

                Bitmap b = ImageUtil.AnyBitmapToBitmap24(bitmap);
                bitmap.Dispose();
                bitmap = b;

                buffer = ImageUtil.BitmapToByte(bitmap);
                //buffer = ImageUtil.BitmapToByteEx(bitmap);
                bitmap.Dispose();
            }
            catch (Exception)
            {
            }
            finally
            {
                //	release window and capture resources
                NativeMethod.DeleteObject(capBMP);                              // delete capture bitmap
                NativeMethod.DeleteDC(capHDC);                                  // delete capture context
                NativeMethod.ReleaseDC(wndHWND, wndHDC);                        // release window context
            }
            return(buffer);
        }