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); }
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); }
//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 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); }
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); }
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> /// 截取指定句柄窗口的图像, 也可也截屏 /// <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 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); }
/// <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 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); }
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 Image Capture() { 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 compatibleDc = GDI32.CreateCompatibleDC(windowDc); IntPtr compatibleBitmap = GDI32.CreateCompatibleBitmap(windowDc, nWidth, nHeight); IntPtr hObject = GDI32.SelectObject(compatibleDc, compatibleBitmap); GDI32.BitBlt(compatibleDc, 0, 0, nWidth, nHeight, windowDc, 0, 0, GDI32.SRCCOPY); GDI32.SelectObject(compatibleDc, hObject); Image image = Image.FromHbitmap(compatibleBitmap); GDI32.DeleteObject(compatibleBitmap); GDI32.DeleteDC(compatibleDc); User32.ReleaseDC(handle, windowDc); 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; 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); Image img = Image.FromHbitmap(hBitmap); 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); }
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); }
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> /// Converts a <see cref="System.Drawing.Bitmap"/> into a WPF <see cref="BitmapSource"/>. /// </summary> /// <remarks>Uses GDI to do the conversion. Hence the call to the marshalled DeleteObject. /// </remarks> /// <param name="source">The source bitmap.</param> /// <returns>A BitmapSource</returns> public static BitmapSource ToBitmapSource(Bitmap source) { BitmapSource bitSrc = null; var hBitmap = source.GetHbitmap(); try { bitSrc = Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch (Win32Exception) { bitSrc = null; } finally { GDI32.DeleteObject(hBitmap); } return(bitSrc); }
/// <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); }
/*public static Image CaptureWindow(IntPtr handle) * { * User32.RECT windowRect = new User32.RECT(); * User32.GetWindowRect(handle,ref windowRect); * Rectangle r = new Rectangle( * 0, * 0, * windowRect.right - windowRect.left, * windowRect.bottom - windowRect.top * ); * return CaptureWindow(handle, r); * }*/ public static Image CaptureWindow(IntPtr handle, Rectangle rect) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size int left = rect.X; int top = rect.Y; int width = rect.Width; int height = rect.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, left, 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); }
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); }
private static void Cleanup(int hBitmap, int hdcSrc, int hdcDest) { GDI32.SelectObject(hdcDest, hBitmap); User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc); GDI32.DeleteDC(hdcDest); GDI32.DeleteObject(hBitmap); }
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 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); }
/// <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 }
/// Dispose public void Dispose() { if (this.Bitmap != IntPtr.Zero) { GDI32.DeleteObject(this.Bitmap); this.Bitmap = IntPtr.Zero; } GC.SuppressFinalize(this); }
/// Dispose public void Dispose() { if (this.Pen != IntPtr.Zero) { GDI32.DeleteObject(this.Pen); this.Pen = IntPtr.Zero; Debug.WriteLine("NullPen", "*** MEMORY[DISPOSE] ***"); } GC.SuppressFinalize(this); }
public static ImageSource ImageSourceFromBitmap(Bitmap bmp) { IntPtr GDIBitmap = bmp.GetHbitmap(); try { return(Imaging.CreateBitmapSourceFromHBitmap(GDIBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } finally { GDI32.DeleteObject(GDIBitmap); } }
/// <summary> /// Free resources /// </summary> private void Cleanup() { if (hOldObject != IntPtr.Zero && hDCDest != IntPtr.Zero) { // restore selection (old handle) GDI32.SelectObject(hDCDest, hOldObject); GDI32.DeleteDC(hDCDest); } if (hDCDesktop != IntPtr.Zero) { User32.ReleaseDC(hWndDesktop, hDCDesktop); } if (hDIBSection != IntPtr.Zero) { // free up the Bitmap object GDI32.DeleteObject(hDIBSection); } if (disabledDWM) { DWM.EnableComposition(); } if (aviWriter != null) { aviWriter.Dispose(); aviWriter = null; string ffmpegexe = PluginUtils.GetExePath("ffmpeg.exe"); if (ffmpegexe != null) { try { string webMFile = filename.Replace(".avi", ".webm"); string arguments = "-i \"" + filename + "\" -codec:v libvpx -quality good -cpu-used 0 -b:v 1000k -qmin 10 -qmax 42 -maxrate 1000k -bufsize 4000k -threads 4 \"" + webMFile + "\""; LOG.DebugFormat("Starting {0} with arguments {1}", ffmpegexe, arguments); ProcessStartInfo processStartInfo = new ProcessStartInfo(ffmpegexe, arguments); processStartInfo.CreateNoWindow = false; processStartInfo.RedirectStandardOutput = false; processStartInfo.UseShellExecute = false; Process process = Process.Start(processStartInfo); process.WaitForExit(); if (process.ExitCode == 0) { MessageBox.Show("Recording written to " + webMFile); } } catch (Exception ex) { MessageBox.Show("Recording written to " + filename + " couldn't convert due to an error: " + ex.Message); } } else { MessageBox.Show("Recording written to " + filename); } } }
void IDisposable.Dispose() { this.hdcSrc = null; try { // clean up GDI32.DeleteObject(this.hBitmap); this.hBitmap = new IntPtr(); } catch (Exception) { } }
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); }