Exemplo n.º 1
0
            public static Bitmap CaptureRuneScapeScreen()
            {
                Bitmap image = null;

                try
                {
                    PinloggerHelpers.RECT r;
                    WinAPI.GetWindowRect(PinloggerHelpers.curhWnd, out r);
                    Point p       = new Point(r.Left, r.Top);
                    int   w       = r.Right - r.Left;
                    int   h       = r.Bottom - r.Top;
                    int   hdcSrc  = WinAPI.GetWindowDC(PinloggerHelpers.curhWnd.ToInt32()),
                          hdcDest = WinAPI.CreateCompatibleDC(hdcSrc),
                          hBitmap = WinAPI.CreateCompatibleBitmap(hdcSrc, w, h);
                    WinAPI.SelectObject(hdcDest, hBitmap);
                    WinAPI.BitBlt(hdcDest, 0, 0, w,
                                  h, hdcSrc, 0, 0, 0x00CC0020);
                    image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
                                       Image.FromHbitmap(new IntPtr(hBitmap)).Width,
                                       Image.FromHbitmap(new IntPtr(hBitmap)).Height);
                    Cleanup(hBitmap, hdcSrc, PinloggerHelpers.curhWnd, hdcDest);
                    GC.Collect();
                }
                catch (Exception ex) { Config.DumpErrorLog(ex, null); }
                return(image);
            }
Exemplo n.º 2
0
        public static Bitmap CaptureWindowScreen(IntPtr hwnd, Rectangle capture_rect)
        {
            WinAPI.RECT win_rect;

            WinAPI.GetWindowRect(hwnd, out win_rect);

            var capture_left   = Math.Max(capture_rect.Left, win_rect.Left);
            var capture_top    = Math.Max(capture_rect.Top, win_rect.Top);
            var capture_right  = Math.Max(capture_rect.Right, win_rect.Right);
            var capture_bottom = Math.Max(capture_rect.Bottom, win_rect.Bottom);

            var win_bmp = new Bitmap(capture_right - capture_left, capture_bottom - capture_top);

            var graphics = Graphics.FromImage(win_bmp);

            var graphics_dc = graphics.GetHdc();

            var win_dc = WinAPI.GetWindowDC(hwnd);

            WinAPI.BitBlt(graphics_dc, 0, 0, win_bmp.Width, win_bmp.Height, win_dc, 0, 0, WinAPI.SRCCOPY | WinAPI.CAPTUREBLT);

            graphics.ReleaseHdc(graphics_dc);
            graphics.Dispose();

            WinAPI.ReleaseDC(WinAPI.Null, win_dc);

            return(win_bmp);
        }
        private Bitmap TakingScreenshotEx2()
        {
            try
            {
                int screenWidth  = Screen.PrimaryScreen.Bounds.Width;
                int screenHeight = Screen.PrimaryScreen.Bounds.Height;

                var _screenshot = new Bitmap(screenWidth, screenHeight);

                Graphics g = Graphics.FromImage(_screenshot);

                IntPtr dc1 = WinAPI.GetDC(WinAPI.GetDesktopWindow());
                IntPtr dc2 = g.GetHdc();

                //Main drawing, copies the screen to the bitmap
                //last number is the copy constant
                WinAPI.BitBlt(dc2, 0, 0, screenWidth, screenHeight, dc1, 0, 0, 13369376);

                //Clean up
                WinAPI.ReleaseDC(WinAPI.GetDesktopWindow(), dc1);
                g.ReleaseHdc(dc2);
                g.Dispose();
                var scaledScreenshot = new Bitmap(_screenshot, new Size(20, 6));
                return(scaledScreenshot);
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        private static void _SetImage(Bitmap image, IntPtr clipboardOwner)
        {
            Bitmap tempImage = new Bitmap(image.Width, image.Height);

            using (Graphics graphics = Graphics.FromImage(tempImage))
            {
                IntPtr hScreenDC       = WinAPI.GetWindowDC(IntPtr.Zero);                                     // 기본적인 Device Context의 속성들을 카피하기 위한 작업
                IntPtr hDestDC         = WinAPI.CreateCompatibleDC(hScreenDC);
                IntPtr hDestBitmap     = WinAPI.CreateCompatibleBitmap(hScreenDC, image.Width, image.Height); // destDC와 destBitmap 모두 반드시 screenDC의 속성들을 기반으로 해야 함.
                IntPtr hPrevDestObject = WinAPI.SelectObject(hDestDC, hDestBitmap);

                IntPtr hSourceDC         = graphics.GetHdc();
                IntPtr hSourceBitmap     = image.GetHbitmap();
                IntPtr hPrevSourceObject = WinAPI.SelectObject(hSourceDC, hSourceBitmap);

                WinAPI.BitBlt(hDestDC, 0, 0, image.Width, image.Height, hSourceDC, 0, 0, WinAPI.SRCCOPY);

                WinAPI.DeleteObject(WinAPI.SelectObject(hSourceDC, hPrevSourceObject));
                WinAPI.SelectObject(hDestDC, hPrevDestObject); // 리턴값 : hDestBitmap
                graphics.ReleaseHdc(hSourceDC);
                WinAPI.DeleteDC(hDestDC);

                bool isClipboardOpen = WinAPI.OpenClipboard(clipboardOwner);
                if (!isClipboardOpen)
                {
                    WinAPI.DeleteObject(hDestBitmap);
                    WinAPI.DeleteObject(hSourceDC);
                    WinAPI.DeleteObject(hSourceBitmap);
                    tempImage.Dispose();
                    throw new CannotOpenException();
                }
                WinAPI.EmptyClipboard();
                WinAPI.SetClipboardData(WinAPI.CF_BITMAP, hDestBitmap);
                WinAPI.CloseClipboard();

                WinAPI.DeleteObject(hDestBitmap);
                WinAPI.DeleteObject(hSourceDC);
                WinAPI.DeleteObject(hSourceBitmap);
            }
            tempImage.Dispose();
        }
Exemplo n.º 5
0
            public static Bitmap CaptureScreen()
            {
                Bitmap image = null;

                try
                {
                    int w       = Screen.PrimaryScreen.Bounds.Size.Width;
                    int h       = Screen.PrimaryScreen.Bounds.Size.Height;
                    int hdcSrc  = WinAPI.GetWindowDC(PinloggerHelpers.curhWnd.ToInt32()),
                        hdcDest = WinAPI.CreateCompatibleDC(hdcSrc),
                        hBitmap = WinAPI.CreateCompatibleBitmap(hdcSrc, w, h);
                    WinAPI.SelectObject(hdcDest, hBitmap);
                    WinAPI.BitBlt(hdcDest, 0, 0, w,
                                  h, hdcSrc, 0, 0, 0x00CC0020);
                    image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
                                       Image.FromHbitmap(new IntPtr(hBitmap)).Width,
                                       Image.FromHbitmap(new IntPtr(hBitmap)).Height);
                    Cleanup(hBitmap, hdcSrc, PinloggerHelpers.curhWnd, hdcDest);
                    GC.Collect();
                }
                catch (Exception ex) { Config.DumpErrorLog(ex, null); }
                return(image);
            }
Exemplo n.º 6
0
        public static Bitmap GetScreenAreaBitmap(int x, int y, int width, int height, PixelFormat pixelFormat)
        {
            var bmp = new Bitmap(width, height);

            using (var graphics = Graphics.FromImage(bmp))
            {
#if USE_WINAPI
                IntPtr hdc_source      = IntPtr.Zero;
                IntPtr hdc_destination = IntPtr.Zero;
#if USE_WINAPI_METHOD_I
                try
                {
                    hdc_source      = WinAPI.GetDC(WinAPI.GetDesktopWindow());                                // hdc_source = WinAPI.GetDC(IntPtr.Zero);
                    hdc_destination = graphics.GetHdc();
                    WinAPI.BitBlt(hdc_destination, 0, 0, width, height, hdc_source, x, y, TernaryRasterOperations.SRCCOPY);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    WinAPI.ReleaseDC(IntPtr.Zero, hdc_source);
                    WinAPI.ReleaseDC(IntPtr.Zero, hdc_destination);
                    WinAPI.DeleteDC(hdc_source);
                    WinAPI.DeleteDC(hdc_destination);                                                       graphics.ReleaseHdc();
                }
#else
                IntPtr compatible_bitmap_handle = IntPtr.Zero;
                try
                {
                    hdc_source               = WinAPI.GetDC(WinAPI.GetDesktopWindow());
                    hdc_destination          = WinAPI.CreateCompatibleDC(hdc_source);
                    compatible_bitmap_handle = WinAPI.CreateCompatibleBitmap(hdc_source, width, height);
                    WinAPI.SelectObject(hdc_destination, compatible_bitmap_handle);
                    WinAPI.BitBlt(hdc_destination, 0, 0, width, height, hdc_source, x, y, TernaryRasterOperations.SRCCOPY);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    WinAPI.ReleaseDC(IntPtr.Zero, hdc_source);
                    WinAPI.ReleaseDC(IntPtr.Zero, hdc_destination);
                    WinAPI.DeleteDC(hdc_source);
                    WinAPI.DeleteDC(hdc_destination);
                    WinAPI.DeleteObject(compatible_bitmap_handle);
                }
#endif
#else
                graphics.CopyFromScreen(x, y, 0, 0, bmp.Size);
#endif

                var cursorInfo = new CURSORINFO();
                cursorInfo.Initialize();
                WinAPI.GetCursorInfo(ref cursorInfo);
                if (cursorInfo.hCursor != IntPtr.Zero)
                {
                    var cursor = new Cursor(cursorInfo.hCursor);
                    //IntPtr hdc_source_2 = WinAPI.GetDC(cursor.Handle);
                    //WinAPI.BitBlt(hdc_destination, cursor.HotSpot.X, cursor.HotSpot.Y, cursor.Size.Width, cursor.Size.Height, hdc_source_2, 0, 0, TernaryRasterOperations.SRCPAINT);
                    //WinAPI.ReleaseDC(cursor.Handle, hdc_source_2); //WinAPI.ReleaseDC(IntPtr.Zero, hdc_source_2);
                    //WinAPI.DeleteDC(hdc_source_2);

                    // FIXME - Cursors.IBeam is white
                    var cursorPosition = new Point(Cursor.Position.X - x, Cursor.Position.Y - y);
                    cursor.Draw(graphics, new Rectangle(cursorPosition, new Size(cursor.Size.Width, cursor.Size.Height)));
                }
            }
            Thread.Sleep(10);
            return(bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), pixelFormat));
        }