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); }
public static Image GetScreenshot(IntPtr hWnd) { IntPtr hdcScreen = WinAPI.GetDC(hWnd); IntPtr hdc = WinAPI.CreateCompatibleDC(hdcScreen); WinAPI.RECT area = new WinAPI.RECT(); WinAPI.GetWindowRect(hWnd, ref area); IntPtr hBitmap = WinAPI.CreateCompatibleBitmap(hdcScreen, area.right - area.left, area.bottom - area.top); WinAPI.SelectObject(hdc, hBitmap); WinAPI.PrintWindow(hWnd, hdc, 0); Image resultOpaque = Image.FromHbitmap(hBitmap); WinAPI.DeleteObject(hBitmap); WinAPI.DeleteDC(hdc); IntPtr hRegion = WinAPI.CreateRectRgn(0, 0, 0, 0); WinAPI.RegionFlags f = (WinAPI.RegionFlags) WinAPI.GetWindowRgn(hWnd, hRegion); Region region = Region.FromHrgn(hRegion); WinAPI.DeleteObject(hRegion); Bitmap result = new Bitmap(resultOpaque.Width, resultOpaque.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(result); g.Clear(Color.Transparent); if (f != WinAPI.RegionFlags.ERROR) g.SetClip(region, System.Drawing.Drawing2D.CombineMode.Replace); if (f != WinAPI.RegionFlags.NULLREGION) g.DrawImageUnscaled(resultOpaque, 0, 0); g.Dispose(); region.Dispose(); resultOpaque.Dispose(); return result; }
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(); }
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); }
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)); }