Beispiel #1
0
        public Point GetWindowPoint(Rectangle rectangle = default(Rectangle))
        {
            // ウィンドウが最小化されていた場合、もとに戻す
            // (ウィンドウが最小化されていると座標を取得できないから)
            this.Restore();
            // ウィンドウを最前面に表示する(アクティブにはしない)
            this.BringWindowToTopWithoutActivation();

            // クライアント領域の左上のスクリーン座標を取得
            NativeCall.POINT screenPoint = new NativeCall.POINT(0, 0);
            NativeCall.ClientToScreen(this.handle, out screenPoint);

            // キャプチャ領域を取得
            NativeCall.RECT clientRect = new NativeCall.RECT();
            NativeCall.GetClientRect(this.handle, out clientRect);
            if (rectangle == default(Rectangle))
            {
                rectangle = new Rectangle(
                    clientRect.left,
                    clientRect.top,
                    clientRect.right - clientRect.left,
                    clientRect.bottom - clientRect.top
                    );
            }
            Point captureStartPoint = new Point(
                screenPoint.X + rectangle.X,
                screenPoint.Y + rectangle.Y
                );

            return(captureStartPoint);
        }
Beispiel #2
0
        /// <summary>
        /// クライアント領域を画像としてキャプチャする
        /// </summary>
        /// <param name="rectangle">クライアント領域内のキャプチャしたい領域。指定しない場合はクライアント領域全体をキャプチャする。</param>
        /// <returns>キャプチャした画像データ</returns>
        public Bitmap CaptureImage(Rectangle rectangle = default(Rectangle))
        {
            // ウィンドウが最小化されていた場合、もとに戻す
            // (ウィンドウが最小化されていると座標を取得できないから)
            this.Restore();
            // ウィンドウを最前面に表示する(アクティブにはしない)
            this.BringWindowToTopWithoutActivation();

            // クライアント領域の左上のスクリーン座標を取得
            NativeCall.POINT screenPoint = new NativeCall.POINT(0, 0);
            NativeCall.ClientToScreen(this.handle, out screenPoint);

            // キャプチャ領域を取得
            NativeCall.RECT clientRect = new NativeCall.RECT();
            NativeCall.GetClientRect(this.handle, out clientRect);
            if (rectangle == default(Rectangle))
            {
                rectangle = new Rectangle(
                    clientRect.left,
                    clientRect.top,
                    clientRect.right - clientRect.left,
                    clientRect.bottom - clientRect.top
                    );
            }
            Point captureStartPoint = new Point(
                screenPoint.X + rectangle.X,
                screenPoint.Y + rectangle.Y
                );
            // キャプチャ実行
            Bitmap   bitmap   = new Bitmap(rectangle.Width, rectangle.Height);
            Graphics graphics = Graphics.FromImage(bitmap);

            using (graphics)
            {
                graphics.CopyFromScreen(captureStartPoint, new Point(0, 0), rectangle.Size);
            }
            return(bitmap);
        }