Beispiel #1
0
            /// <summary>
            /// Draw a rectangle over a window.
            /// </summary>
            /// <param name="hWnd">Window's handle.</param>
            /// <param name="color">Rectangle's color.</param>
            /// 
            /// <!-- Source: http://www.codegod.com/find-window-and-highlight-it-with-win32-AID140.aspx -->
            public static void DrawRectangle(IntPtr hWnd, Color color)
            {
                const float penWidth = 2F;

                var rc = new RECT();
                PInvoke.GetWindowRect(hWnd, ref rc);

                var hDC = PInvoke.GetWindowDC(hWnd);

                if (hDC != IntPtr.Zero)
                {
                    using (var pen = new Pen(color, penWidth))
                    {
                        using (var g = Graphics.FromHdc(hDC))
                        {
                            var transp1 = Color.FromArgb(122, color.R, color.G, color.B);
                            var transp2 = Color.Transparent;
                            var rect = new Rectangle(0, 0, rc.Right - rc.Left - (int)penWidth,
                                                     rc.Bottom - rc.Top - (int)penWidth);

                            var transBrush = new LinearGradientBrush(rect, transp2, transp1, 45, true);
                            g.DrawRectangle(pen, rect);
                            g.FillRectangle(transBrush, rect);

                        }
                    }
                }
                PInvoke.ReleaseDC(hWnd, hDC);
            }
Beispiel #2
0
            /// <summary>
            /// Takes a screenshot of desired window.
            /// </summary>
            /// <param name="hWnd">Window/s handle (IntPtr.Zero for desktop).</param>
            /// <returns>Window's screenshot.</returns>
            /// 
            /// <!-- Source: http://www.developerfusion.com/code/4630/capture-a-screen-shot/ -->
            public static Bitmap Screenshot(IntPtr hWnd)
            {
                if (hWnd == IntPtr.Zero)
                    hWnd = PInvoke.GetDesktopWindow();

                // get te hDC of the target window
                var hdcSrc = PInvoke.GetWindowDC(hWnd);
                // get the size
                var windowRect = new RECT();
                PInvoke.GetWindowRect(hWnd, ref windowRect);
                var width = windowRect.Right - windowRect.Left;
                var height = windowRect.Bottom - windowRect.Top;
                // create a device context we can copy to
                var hdcDest = PInvoke.CreateCompatibleDC(hdcSrc);
                // create a bitmap we can copy it to,
                // using GetDeviceCaps to get the width/height
                var hBitmap = PInvoke.CreateCompatibleBitmap(hdcSrc, width, height);
                // select the bitmap object
                var hOld = PInvoke.SelectObject(hdcDest, hBitmap);
                // bitblt over
                PInvoke.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, TernaryRasterOperations.SRCCOPY);
                // restore selection
                PInvoke.SelectObject(hdcDest, hOld);
                // clean up
                PInvoke.DeleteDC(hdcDest);
                PInvoke.ReleaseDC(hWnd, hdcSrc);
                // get a .NET image object for it
                var bmp = Image.FromHbitmap(hBitmap);
                // free up the Bitmap object
                PInvoke.DeleteObject(hBitmap);

                //if (!bmp.Size.IsEmpty)
                //    bmp.Save("IMG\\scrn" + Environment.TickCount + ".bmp", ImageFormat.Bmp);

                return bmp;
            }
Beispiel #3
0
            /// <summary>
            /// Takes a screenshot of the desired window.
            /// </summary>
            /// <param name="hWnd">Window's handle (IntPtr.Zero for desktop).</param>
            /// <returns>Window's screenshot.</returns>
            ///<remarks>Very slow.. Use 'Screenshot' method instead.</remarks>
            public static Bitmap ScreenshotWithGetPixel(IntPtr hWnd)
            {
                if (hWnd == IntPtr.Zero)
                    hWnd = PInvoke.GetDesktopWindow();

                var hdcSrc = PInvoke.GetWindowDC(hWnd);

                var windowRect = new RECT();
                PInvoke.GetWindowRect(hWnd, ref windowRect);
                var width = windowRect.Right - windowRect.Left;
                var height = windowRect.Bottom - windowRect.Top;

                var bmp = new Bitmap(width, height);

                for (int x = 0; x < width; x++)
                    for (int y = 0; y < height; y++)
                    {
                        bmp.SetPixel(x, y, Utilities.GetColorFromABGR(PInvoke.GetPixel(hdcSrc, x, y)));
                    }
                return bmp;
            }