public static Bitmap GetDesktop() { int screenX; int screenY; IntPtr hBmp; IntPtr hdcScreen = User32.GetDC(User32.GetDesktopWindow()); IntPtr hdcCompatible = GDI32.CreateCompatibleDC(hdcScreen); screenX = User32.GetSystemMetrics(0); screenY = User32.GetSystemMetrics(1); hBmp = GDI32.CreateCompatibleBitmap(hdcScreen, screenX, screenY); if (hBmp != IntPtr.Zero) { IntPtr hOldBmp = (IntPtr)GDI32.SelectObject(hdcCompatible, hBmp); GDI32.BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, GDI32.TernaryRasterOperations.SRCCOPY); GDI32.SelectObject(hdcCompatible, hOldBmp); GDI32.DeleteDC(hdcCompatible); User32.ReleaseDC(User32.GetDesktopWindow(), hdcScreen); Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp); GDI32.DeleteObject(hBmp); GC.Collect(); return(bmp); } return(null); }
public static Bitmap getRegion(Rectangle rect) { int x = rect.X; int y = rect.Y; int width = rect.Width; int height = rect.Height; IntPtr hBmp; IntPtr hdcScreen = User32.GetDC(User32.GetDesktopWindow()); IntPtr hdcCompatible = GDI32.CreateCompatibleDC(hdcScreen); hBmp = GDI32.CreateCompatibleBitmap(hdcScreen, width, height); if (hBmp != IntPtr.Zero) { IntPtr hOldBmp = (IntPtr)GDI32.SelectObject(hdcCompatible, hBmp); GDI32.BitBlt(hdcCompatible, 0, 0, width, height, hdcScreen, x, y, GDI32.TernaryRasterOperations.SRCCOPY); GDI32.SelectObject(hdcCompatible, hOldBmp); GDI32.DeleteDC(hdcCompatible); User32.ReleaseDC(User32.GetDesktopWindow(), hdcScreen); Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp); GDI32.DeleteObject(hBmp); GC.Collect(); return(bmp); } return(null); }
static private Image CaptureWindow(IntPtr hWnd) { IntPtr hdcSrc = User32.GetWindowDC(hWnd); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(hWnd, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); User32.PrintWindow(hWnd, hdcDest, 0); GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(hWnd, hdcSrc); Image img = Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); return(img); }
//Get screenshot on the center of game public static System.Drawing.Image CaptureWindow(string name, bool followMouse) { if (Process.GetProcessesByName(name).Count() == 0) { giveErrorMessage($"Parece que você fechou o {name}..."); } IntPtr handle = Process.GetProcessesByName(name)[0].MainWindowHandle; IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); screen_width = windowRect.right - windowRect.left; screen_height = windowRect.bottom - windowRect.top; IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, size.X, size.Y); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); if (followMouse) { GDI32.BitBlt(hdcDest, 0, 0, size.X, size.Y, hdcSrc, coordinates.X - size.X / 2, coordinates.Y - size.Y / 2, GDI32.SRCCOPY); } else { GDI32.BitBlt(hdcDest, 0, 0, size.X, size.Y, hdcSrc, screen_width / 2 - size.X / 2, screen_height / 2 - size.Y / 2, GDI32.SRCCOPY); } GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); System.Drawing.Image img = System.Drawing.Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); return(img); }
public override Bitmap PrintWindow() { //Rect rc; //// TODO: This might be stuck forever. Use timer instead? //int tries = 100; //while (!GetWindowRect(Screen, out rc) && tries-- > 0) { }; //if (tries <= 0) //{ // throw new Exception("Failed to Print Window"); //} //Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format24bppRgb); //Graphics gfxBmp = Graphics.FromImage(bmp); //IntPtr hdcBitmap = gfxBmp.GetHdc(); //PrintWindow(Screen, hdcBitmap, 0); //gfxBmp.ReleaseHdc(hdcBitmap); //gfxBmp.Dispose(); //bmp.Save("C:\\TestWin32\\test.png", ImageFormat.Png); //return bmp; // get te hDC of the target window IntPtr hdcSrc = GetWindowDC(Screen); // get the size Rect windowRect; GetWindowRect(Screen, out windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); ReleaseDC(Screen, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); ((Bitmap)img).Save("C:\\TestWin32\\test.png", ImageFormat.Png); ((Bitmap)img).Save("C:\\TestWin32\\test2.jpeg", ImageFormat.Jpeg); return((Bitmap)img); }
/// <summary> /// Create un objeto imagen que contiene una captura de pantalla de la ventana actual /// </summary> /// <param name="handle">La instancia Handle de la ventana (En windows forms, es obtenida a traves de la propiedad Handle)</param> /// <returns></returns> public Image CaptureWindow(IntPtr handle) { // obtener hDC de la ventana deseada IntPtr hdcSrc = User32.GetWindowDC(handle); // Obtener el tamano User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // Crea un contexto en el que se copiara la imagen IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // Seleccionar el objeto bitmap IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // finalizar bitblt GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // Restaurar seleccion GDI32.SelectObject(hdcDest, hOld); // Limpiar GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // Obtener una imagen .NET image del bitmap Image img = Image.FromHbitmap(hBitmap); // Liberar objeto Bitmab GDI32.DeleteObject(hBitmap); return(img); }
/// <summary> /// 截取指定句柄窗口的图像, 也可也截屏 /// <para>当然 当前封装是依赖传入参数获取窗口大小,也可以使用API自动获取</para> /// </summary> /// <param name="handle"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Bitmap CaptureWindow(IntPtr handle, int width, int height) { try { // get the hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Bitmap img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(null); }
public Bitmap CaptureRegion(IntPtr handle, int x, int y, int width, int height) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Bitmap img = Bitmap.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); }
//=================================================================== // スクリーンキャプチャ //=================================================================== /// スクリーンキャプチャした結果をHBitmapに格納する /// @warning 返り値はかならずusingと一緒に使うかDispose()すること /// @return スクリーンキャプチャした結果のHBitmap public BitmapHandle Execute() { // Windowチェック if (!Common.Utilities.IsWindowValid(this.Window)) return null; // BitBlt var window = this.Window; var windowDC = User32.GetDC(window); var capturedDC = GDI32.CreateCompatibleDC(windowDC); var capturedBitmap = GDI32.CreateCompatibleBitmap(windowDC, this.ClippingWidth, this.ClippingHeight); { var originalBitmap = GDI32.SelectObject(capturedDC, capturedBitmap); GDI32.BitBlt(capturedDC, 0, 0, this.ClippingWidth, this.ClippingHeight, windowDC, this.ClippingX, this.ClippingY, this.ShowLayeredWindow ? GDI32.SRCCOPY | GDI32.CAPTUREBLT : GDI32.SRCCOPY); GDI32.SelectObject(capturedDC, originalBitmap); } GDI32.DeleteDC(capturedDC); User32.ReleaseDC(window, windowDC); /// @todo(me) マウスカーソルの合成・・・?いるか? if (this.ShowCursor) { // nop } return new BitmapHandle(capturedBitmap); }
private Image CaptureWindow(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int borderSize = Math.Abs(this.Width - this.ClientRectangle.Width) / 2; int titleBarSize = Math.Abs(this.Height - borderSize - this.ClientRectangle.Height); int width = windowRect.right - windowRect.left - 2 * borderSize; int height = windowRect.bottom - windowRect.top - titleBarSize - borderSize; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over //GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, borderSize, titleBarSize, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); }
public static Bitmap CopyFromScreen(IntPtr bdoHandle) { User32.SIZE size; var hDc = User32.GetDC(bdoHandle); var hMemDc = GDI32.CreateCompatibleDC(hDc); size.cx = User32.GetSystemMetrics(User32.SM_CXSCREEN); size.cy = User32.GetSystemMetrics(User32.SM_CYSCREEN); var hBitmap = GDI32.CreateCompatibleBitmap(hDc, size.cx, size.cy); if (hBitmap == IntPtr.Zero) { return(null); } var hOld = GDI32.SelectObject(hMemDc, hBitmap); GDI32.BitBlt(hMemDc, 0, 0, size.cx, size.cy, hDc, 0, 0, GDI32.SRCCOPY); GDI32.SelectObject(hMemDc, hOld); GDI32.DeleteDC(hMemDc); User32.ReleaseDC(bdoHandle, hDc); var bmp = Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); GC.Collect(); return(bmp); }
/// <summary> /// Creates an Image object containing a screen shot of a specific part (rectangle) of awindow /// </summary> /// <param name="handle">The handle to the window. /// (In windows forms, this is obtained by the Handle property)</param> /// <param name="rectangle">The rectangle specifying the part</param> /// <returns></returns> public Image CaptureWindowRectangle(IntPtr handle, Rectangle rectangle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size int width = rectangle.Width; int height = rectangle.Height; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, rectangle.Width, rectangle.Height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, rectangle.Width, rectangle.Height, hdcSrc, rectangle.Left, rectangle.Top, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); }
public static Image CaptureWindow(IntPtr handle) { int width, height; { User32.RECT windowRect; User32.GetWindowRect(handle, out windowRect); width = windowRect.right - windowRect.left; height = windowRect.bottom - windowRect.top; } Debug.Assert(width > 0 && height > 0); Image img = null; { IntPtr hdcSrc = User32.GetWindowDC(handle); IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); { IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCDOPY | GDI32.CAPTUREBLT); GDI32.SelectObject(hdcDest, hOld); img = Image.FromHbitmap(hBitmap); } GDI32.DeleteObject(hBitmap); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); } return(img); }
private static System.Drawing.Bitmap CaptureWindow(IntPtr handle, Rectangle region) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = region.Width; int height = region.Height; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, region.X, region.Y, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it System.Drawing.Bitmap img = System.Drawing.Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); }
public static Bitmap CaptureScreen() { IntPtr handle = User32.GetDesktopWindow(); IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width; int height; if ((Screen.PrimaryScreen.WorkingArea.Width == 1536) && (Screen.PrimaryScreen.WorkingArea.Height == 824)) { width = 1920; height = 1080; } else { width = (Screen.PrimaryScreen.WorkingArea.Width); height = (Screen.PrimaryScreen.WorkingArea.Height); } IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); Bitmap img = Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); GC.Collect(); return(img); }
public System.Drawing.Image CaptureWindow(IntPtr handle, int picWidth) { try { //User32.SetForegroundWindow(handle); Rectangle bRect = new Rectangle(); IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT wRect = new User32.RECT(); User32.GetWindowRect(handle, ref wRect); bRect.Width = picWidth; bRect.Height = picWidth; bRect.X = (wRect.right - wRect.left) / 2 - (picWidth / 2); bRect.Y = (wRect.bottom - wRect.top) / 2 - (picWidth / 2); //bRect.Width = picWidth; //bRect.Height = picWidth; //bRect.X = 0;// (wRect.right - wRect.left) / 2 - (picWidth / 2); //bRect.Y = 0;// (wRect.bottom - wRect.top) / 2 - (picWidth / 2); IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, bRect.Width, bRect.Height); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, bRect.Width, bRect.Height, hdcSrc, bRect.Left, bRect.Top, GDI32.SRCCOPY); GDI32.SelectObject(hdcDest, hOld); System.Drawing.Image img = System.Drawing.Image.FromHbitmap(hBitmap); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); GDI32.DeleteObject(hBitmap); return(img); } catch (Exception ex) { return(null); } }
private Image PrintScreen(IntPtr hWnd) { //https://blog.csdn.net/spiderlily/article/details/8548470 // User32.dll PrintWindow IntPtr hscrdc = User32.GetWindowDC(hWnd); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(hWnd, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; IntPtr hbitmap = GDI32.CreateCompatibleBitmap(hscrdc, width, height); IntPtr hmemdc = GDI32.CreateCompatibleDC(hscrdc); GDI32.SelectObject(hmemdc, hbitmap); bool re = User32.PrintWindow(hWnd, hmemdc, 0); Bitmap bmp = null; if (re) { bmp = Bitmap.FromHbitmap(hbitmap); } GDI32.DeleteObject(hbitmap); GDI32.DeleteDC(hmemdc); User32.ReleaseDC(hWnd, hscrdc); return(bmp); }
public System.Drawing.Image CaptureWindow(IntPtr handle) { IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); System.Drawing.Image image = System.Drawing.Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); return(image); }
public Image CaptureWindow(IntPtr handle) { IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); Image img = Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); return(img); }
/// <summary> /// Creates an Image object containing a screen shot of a specific window /// </summary> /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param> /// <returns></returns> public static Image CaptureWindow(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); }
public Image CaptureWindow(IntPtr handle) { IntPtr windowDC = User32.GetWindowDC(handle); User32.RECT rect = new User32.RECT(); User32.GetWindowRect(handle, ref rect); int nWidth = rect.right - rect.left; int nHeight = rect.bottom - rect.top; IntPtr hDC = GDI32.CreateCompatibleDC(windowDC); IntPtr hObject = GDI32.CreateCompatibleBitmap(windowDC, nWidth, nHeight); IntPtr ptr4 = GDI32.SelectObject(hDC, hObject); GDI32.BitBlt(hDC, 0, 0, nWidth, nHeight, windowDC, 0, 0, 0xcc0020); GDI32.SelectObject(hDC, ptr4); GDI32.DeleteDC(hDC); User32.ReleaseDC(handle, windowDC); Image image = Image.FromHbitmap(hObject); GDI32.DeleteObject(hObject); return(image); }
/// <summary> /// Captures the window or part thereof to a bitmap image. /// </summary> /// <param name="wndHWND">window handle</param> /// <param name="x">x location in window</param> /// <param name="y">y location in window</param> /// <param name="width">width of capture area</param> /// <param name="height">height of capture area</param> /// <returns>window bitmap</returns> public static Bitmap Window(IntPtr wndHWND, int x, int y, int width, int height) { IntPtr wndHDC = USER32.GetDC(wndHWND); // get context for window // create compatibile capture context and bitmap IntPtr capHDC = GDI32.CreateCompatibleDC(wndHDC); IntPtr capBMP = GDI32.CreateCompatibleBitmap(wndHDC, width, height); // make sure bitmap non-zero if (capBMP == IntPtr.Zero) // if no compatible bitmap { USER32.ReleaseDC(wndHWND, wndHDC); // release window context GDI32.DeleteDC(capHDC); // delete capture context return(null); // return null bitmap } // select compatible bitmap in compatible context // copy window context to compatible context // select previous bitmap back into compatible context IntPtr prvHDC = (IntPtr)GDI32.SelectObject(capHDC, capBMP); GDI32.BitBlt(capHDC, 0, 0, width, height, wndHDC, x, y, GDI32.SRCCOPY); GDI32.SelectObject(capHDC, prvHDC); // create GDI+ bitmap for window Bitmap bmp = System.Drawing.Image.FromHbitmap(capBMP); // release window and capture resources USER32.ReleaseDC(wndHWND, wndHDC); // release window context GDI32.DeleteDC(capHDC); // delete capture context GDI32.DeleteObject(capBMP); // delete capture bitmap // return bitmap image to user return(bmp); // return bitmap }
static IntPtr getCompatibleDeviceContext(IntPtr hdcSrc) { // create a device context we can copy to // IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); if (_hdcDest == IntPtr.Zero) { _hdcDest = GDI32.CreateCompatibleDC(hdcSrc); } return(_hdcDest); }
// screen caputre method 1 functions public void CaptureScreen(string fileName, ImageFormat imageFormat) { int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()), hdcDest = GDI32.CreateCompatibleDC(hdcSrc), hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10)); GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10), hdcSrc, 0, 0, 0x00CC0020); SaveImageAs(hBitmap, fileName, imageFormat); Cleanup(hBitmap, hdcSrc, hdcDest); }
public static Bitmap CaptureRectangle(Rectangle captureBounds) { IntPtr hDesktop = User32.GetDesktopWindow(); IntPtr hDC = User32.GetWindowDC(hDesktop); IntPtr hDest = GDI32.CreateCompatibleDC(hDC); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hDC, captureBounds.Width, captureBounds.Height); IntPtr hOldBitmap = GDI32.SelectObject(hDest, hBitmap); GDI32.BitBlt(hDest, 0, 0, captureBounds.Width, captureBounds.Height, hDC, captureBounds.X, captureBounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Bitmap bitmap = null; bool isRegionEmpty = true; using (var graphics = Graphics.FromHwnd(hDesktop)) { isRegionEmpty = WindowCapture.IsRegionEmpty(graphics, captureBounds); } if (isRegionEmpty) { bitmap = Bitmap.FromHbitmap(hBitmap); } else { float xDpi = 96F, yDpi = 96F; using (var tmp = Bitmap.FromHbitmap(hBitmap)) { xDpi = tmp.HorizontalResolution; yDpi = tmp.VerticalResolution; } bitmap = WindowCapture.CreateEmpty(captureBounds.Width, captureBounds.Height, PixelFormat.Format32bppArgb, Color.Transparent, xDpi, yDpi); using (var graphics = Graphics.FromImage(bitmap)) { foreach (var screen in Screen.AllScreens) { var bounds = screen.Bounds; bounds.Offset(-captureBounds.X, -captureBounds.Y); graphics.DrawImage(bitmap, bounds, bounds.X, bounds.Y, bounds.Width, bounds.Height, GraphicsUnit.Pixel); } } } GDI32.SelectObject(hDest, hOldBitmap); GDI32.DeleteObject(hBitmap); GDI32.DeleteDC(hDest); User32.ReleaseDC(hDesktop, hDC); return(bitmap); }
/// <summary> /// Creates an Image object containing a screen shot of a specific window /// </summary> /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param> /// <returns></returns> private static void CaptureWindow(IntPtr handle) { // memoryStream.Position = 0; // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); width = windowRect.right - windowRect.left; height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Bitmap bmp = Bitmap.FromHbitmap(hBitmap); Rectangle rect = new Rectangle(0, 0, width, height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); unsafe { byte *p = (byte *)bmpData.Scan0.ToPointer(); for (int y = 0, k = 0; y < height; y++) { for (int x = 0; x < width; x++) { byte r = p[2]; byte g = p[1]; byte b = p[0]; p += 4; screenBitmap[k++] = (UInt32)(((r << 16) | (g << 8) | b) | 0xff000000); } p += bmpData.Stride - width * 4; } } // free up the Bitmap object bmp.UnlockBits(bmpData); bmp.Dispose(); GDI32.DeleteObject(hBitmap); }
public Pix CaptureWindowPix(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it //Image img = Image.FromHbitmap(hBitmap); int bitsPerPixel = ((int)PixelFormat.Format32bppArgb & 0xff00) >> 8; int bytesPerPixel = (bitsPerPixel + 7) / 8; int stride = 4 * ((width * bytesPerPixel + 3) / 4); //Bitmap intermediate = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, hBitmap); //Pix img = PixConverter.ToPix(intermediate); Bitmap orig = Image.FromHbitmap(hBitmap); Bitmap clone = new Bitmap(orig.Width, orig.Height, PixelFormat.Format32bppArgb); using (Graphics gr = Graphics.FromImage(clone)) { gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height)); } orig.Dispose(); Pix img = PixConverter.ToPix(clone); //Pix img = PixConverter.ToPix(Image.FromHbitmap(hBitmap)); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); }
public Image CaptureGameScreen(IntPtr handle) { int windowTopOffset = 55; IntPtr hdcSrc = User32.GetWindowDC(handle); User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left - 20; int height = windowRect.bottom - windowRect.top - windowTopOffset - 8; int diffx = 0; int offsetx = 0; int diffy = 0; int offsety = 0; if (height / width < 0.9375) { var idealWidth = (int)(height * 1.0667); diffx = width - idealWidth > 0 ? width - idealWidth : 0; offsetx = diffx / 2; } else if (height / width > 0.9375) { var idealHeight = (int)(width * 0.9375); diffy = height - idealHeight > 0 ? height - idealHeight : 0; offsety = diffy / 2; } IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width - diffx, height - diffy); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width - offsetx, height - offsety, hdcSrc, 10 + offsetx, windowTopOffset + offsety, GDI32.SRCCOPY); GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); Image img = Image.FromHbitmap(hBitmap); Debug.Print(img.HorizontalResolution.ToString()); Debug.Print(img.VerticalResolution.ToString()); Debug.Print(img.Width.ToString()); Debug.Print(img.Height.ToString()); GDI32.DeleteObject(hBitmap); return(ResizeImage(img, 256, 240)); }
public static Bitmap PrintWindow(IntPtr hWnd) { IntPtr hscrdc = User32.GetWindowDC(hWnd); Control control = Control.FromHandle(hWnd); IntPtr hbitmap = GDI32.CreateCompatibleBitmap(hscrdc, control.Width, control.Height); IntPtr hmemdc = GDI32.CreateCompatibleDC(hscrdc); GDI32.SelectObject(hmemdc, hbitmap); User32.PrintWindow(hWnd, hmemdc, 0); Bitmap bmp = Bitmap.FromHbitmap(hbitmap); GDI32.DeleteDC(hscrdc); //删除用过的对象 GDI32.DeleteDC(hmemdc); //删除用过的对象 return(bmp); }
/// <summary> /// Creates an Image object containing a screen shot of a specific window /// </summary> /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param> /// <returns></returns> public (Bitmap Image, Rectangle WindowRect) CaptureWindow(IntPtr handle, Rectangle?rect = null) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; var windowRect2 = new Rectangle(windowRect.left, windowRect.top, width, height); Rectangle captureArea; if (rect.HasValue) { captureArea = rect.Value; } else { captureArea = new Rectangle(0, 0, width, height); } // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, captureArea.Width, captureArea.Height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, captureArea.Width, captureArea.Height, hdcSrc, captureArea.X, captureArea.Y, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // get a .NET image object for it Bitmap img = Bitmap.FromHbitmap(hBitmap); //Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img, windowRect2); }