/// <summary> /// A képernyő egy adott régiójáról készít képernyőképet. /// </summary> /// <param name="hWnd">Az ablak Handle</param> /// <param name="x">x koordináta</param> /// <param name="y">y koordináta</param> /// <param name="width">A kép szélessége (A terület szélessége)</param> /// <param name="height">A kép/terület magassága</param> /// <param name="addToClipboard">Nem használt, jövőbeni funkciónak fenntartott</param> /// <returns>BitmapSource amelyből a kép kinyerhető</returns> /// <example> /// <code lang='cs'> /// bitmapsource = CaptureRegion(((HwndSource)HwndSource.FromVisual(Application.Current.MainWindow)).Handle, /// (int)Application.Current.MainWindow.Left, (int)Application.Current.MainWindow.Top, /// (int)Application.Current.MainWindow.Width, (int)Application.Current.MainWindow.Height, false); /// </code> /// </example> public static BitmapSource CaptureRegion( IntPtr hWnd, int x, int y, int width, int height, bool addToClipboard) { IntPtr sourceDC = IntPtr.Zero; IntPtr targetDC = IntPtr.Zero; IntPtr compatibleBitmapHandle = IntPtr.Zero; BitmapSource bitmap = null; try { sourceDC = User32.GetDC(User32.GetDesktopWindow()); targetDC = Gdi32.CreateCompatibleDC(sourceDC); compatibleBitmapHandle = Gdi32.CreateCompatibleBitmap(sourceDC, width, height); Gdi32.SelectObject(targetDC, compatibleBitmapHandle); Gdi32.BitBlt(targetDC, 0, 0, width, height, sourceDC, x, y, Gdi32.SRCCOPY); bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( compatibleBitmapHandle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch (Exception ex) { throw new ScreenCaptureException(string.Format("Error capturing region {0},{1},{2},{3}", x, y, width, height), ex); } finally { Gdi32.DeleteObject(compatibleBitmapHandle); User32.ReleaseDC(IntPtr.Zero, sourceDC); User32.ReleaseDC(IntPtr.Zero, targetDC); } return(bitmap); }
public Image CaptureWindow(IntPtr handle) { // get te hDC of the target window var hdcSrc = User32.GetWindowDC(handle); // get the size var windowRect = new User32.Rect(); User32.GetWindowRect(handle, ref windowRect); var width = windowRect.right - windowRect.left; var height = windowRect.bottom - windowRect.top; // create a device context we can copy to var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height var hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object var 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 static Bitmap Capture(IntPtr Window) { IntPtr SourceDC = User32.GetWindowDC(Window), MemoryDC = Gdi32.CreateCompatibleDC(SourceDC); var rect = new RECT(); User32.GetWindowRect(Window, ref rect); int Width = rect.Right - rect.Left, Height = rect.Bottom - rect.Top; // Create a bitmap we can copy it to var hBmp = Gdi32.CreateCompatibleBitmap(SourceDC, Width, Height); try { // select the bitmap object var hOld = Gdi32.SelectObject(MemoryDC, hBmp); // bitblt over Gdi32.BitBlt(MemoryDC, 0, 0, Width, Height, SourceDC, 0, 0, CopyPixelOperation.SourceCopy); // restore selection Gdi32.SelectObject(MemoryDC, hOld); // get a .NET image object for it return(Bitmap.FromHbitmap(hBmp)); } finally { Gdi32.DeleteObject(hBmp); } }
/// <summary> /// Creates a Bitmap containing the screen of the window /// </summary> /// <param name="handle">The handle to the window.</param> /// <returns>Bitmap containing the screen of the window</returns> static private Bitmap CaptureWindowByHandle(IntPtr handle) { IntPtr hSrc; IntPtr hDest; IntPtr hBitmap; IntPtr hOld; //create the rectangle and get image size User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int height = windowRect.bottom - windowRect.top; int width = windowRect.right - windowRect.left; //get all handle device context hSrc = User32.GetWindowDC(handle); hDest = Gdi32.CreateCompatibleDC(hSrc); hBitmap = Gdi32.CreateCompatibleBitmap(hSrc, width, height); hOld = Gdi32.SelectObject(hDest, hBitmap); //get the image Gdi32.BitBlt(hDest, 0, 0, width, height, hSrc, 0, 0, Gdi32.SRCC); Gdi32.SelectObject(hDest, hOld); Bitmap img = Image.FromHbitmap(hBitmap); //free the memory Gdi32.DeleteDC(hDest); User32.ReleaseDC(handle, hSrc); Gdi32.DeleteObject(hBitmap); //return the image return(img); }
//通过鼠标前后两个位置来获取图片 public Image GetPic_ByMouse(IntPtr hWnd) { Image img; // 获取设备上下文环境句柄 IntPtr hscrdc = User32.GetWindowDC(hWnd); // 创建一个与指定设备兼容的内存设备上下文环境(DC) IntPtr hmemdc = Gdi32.CreateCompatibleDC(hscrdc); IntPtr myMemdc = Gdi32.CreateCompatibleDC(hscrdc); // 返回指定窗体的矩形尺寸 User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(hWnd, ref windowRect); // 返回指定设备环境句柄对应的位图区域句柄 IntPtr myBitmap = Gdi32.CreateCompatibleBitmap(hscrdc, width, height); IntPtr hbitmap = Gdi32.CreateCompatibleBitmap(hscrdc, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top); //把位图选进内存DC Gdi32.SelectObject(hmemdc, hbitmap); Gdi32.SelectObject(myMemdc, myBitmap); Gdi32.BitBlt(myMemdc, 0, 0, width, height, hscrdc, Point_push.X, Point_push.Y, Gdi32.SRCCOPY); img = Image.FromHbitmap(myBitmap); Gdi32.DeleteDC(hscrdc); Gdi32.DeleteDC(hmemdc); Gdi32.DeleteDC(myMemdc); return(img); }
private static BitmapSource CaptureRegion(IntPtr hWnd, int x, int y, int width, int height, bool addToClipboard) { IntPtr sourceDC = IntPtr.Zero; IntPtr targetDC = IntPtr.Zero; IntPtr compatibleBitmapHandle = IntPtr.Zero; BitmapSource bitmap = null; sourceDC = User32.GetDC(User32.GetDesktopWindow()); targetDC = Gdi32.CreateCompatibleDC(sourceDC); compatibleBitmapHandle = Gdi32.CreateCompatibleBitmap(sourceDC, width, height); Gdi32.SelectObject(targetDC, compatibleBitmapHandle); Gdi32.BitBlt(targetDC, 0, 0, width, height, sourceDC, x, y, Gdi32.SRCCOPY); bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( compatibleBitmapHandle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (addToClipboard) { IDataObject data = new DataObject(); data.SetData(DataFormats.Dib, bitmap, false); Clipboard.SetDataObject(data, false); } Gdi32.DeleteObject(compatibleBitmapHandle); User32.ReleaseDC(IntPtr.Zero, sourceDC); User32.ReleaseDC(IntPtr.Zero, targetDC); return(bitmap); }
public RegionProvider(Rectangle Region, bool IncludeCursor, Func <Point> LocationFunc = null) { _region = Region; _includeCursor = IncludeCursor; _locationFunc = LocationFunc ?? (() => Region.Location); // Width and Height must be even. // Use these for Bitmap size, but capture as per region size Width = _region.Width; if (Width % 2 == 1) { ++Width; } Height = _region.Height; if (Height % 2 == 1) { ++Height; } PointTransform = P => new Point(P.X - _region.X, P.Y - _region.Y); _hdcSrc = User32.GetDC(IntPtr.Zero); _hdcDest = Gdi32.CreateCompatibleDC(_hdcSrc); _hBitmap = Gdi32.CreateCompatibleBitmap(_hdcSrc, Width, Height); Gdi32.SelectObject(_hdcDest, _hBitmap); }
/// <summary> /// Creates screenshot of all available screens. <br/> /// If <paramref name="cropRectangle"/> is not null, returns image of this region. /// </summary> /// <param name="cropRectangle"></param> /// <returns></returns> public static Bitmap Shot(Rectangle?cropRectangle = null) { var rectangle = (cropRectangle ?? GetPhysicalScreenRectangle()).Normalize(); var window = User32.GetDesktopWindow(); using var dc = User32.GetWindowDC(window); using var toDc = Gdi32.CreateCompatibleDC(dc); var hBmp = Gdi32.CreateCompatibleBitmap(dc, rectangle.Width, rectangle.Height); var hOldBmp = Gdi32.SelectObject(toDc, hBmp); // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags Gdi32.BitBlt(toDc.DangerousGetHandle(), 0, 0, rectangle.Width, rectangle.Height, dc.DangerousGetHandle(), rectangle.X, rectangle.Y, (int)(CopyPixelOperation.CaptureBlt | CopyPixelOperation.SourceCopy)); var bitmap = Image.FromHbitmap(hBmp); Gdi32.SelectObject(toDc, hOldBmp); Gdi32.DeleteObject(hBmp).Check(); Gdi32.DeleteDC(toDc).Check(); //? User32.ReleaseDC(window, dc.DangerousGetHandle()).Check(); //? return(bitmap); }
public override void Start(int delay, int left, int top, int width, int height, double scale, ProjectInfo project) { base.Start(delay, left, top, width, height, scale, project); #region Pointers //http://winprog.org/tutorial/bitmaps.html //_desktopWindow = User32.GetDesktopWindow(); WindowDeviceContext = User32.GetWindowDC(_desktopWindow); CompatibleDeviceContext = Gdi32.CreateCompatibleDC(WindowDeviceContext); CompatibleBitmap = Gdi32.CreateCompatibleBitmap(WindowDeviceContext, Width, Height); _oldBitmap = Gdi32.SelectObject(CompatibleDeviceContext, CompatibleBitmap); #endregion var pixelOp = CopyPixelOperations.SourceCopy; //If not in a remote desktop connection or if the improvement was disabled, capture layered windows too. if (!System.Windows.Forms.SystemInformation.TerminalServerSession || !UserSettings.All.RemoteImprovement) { pixelOp |= CopyPixelOperations.CaptureBlt; } PixelOperations = pixelOp; }
public Bitmap CaptureWindowGDI(IntPtr handle) { var hdcSrc = User32.GetWindowDC(handle); var windowRect = new RECT(); User32.GetWindowRect(handle, ref windowRect); var width = windowRect.right - windowRect.left; var height = windowRect.bottom - windowRect.top; var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); var hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); var hOld = Gdi32.SelectObject(hdcDest, hBitmap); Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 13369376); Gdi32.SelectObject(hdcDest, hOld); Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); var image = Image.FromHbitmap(hBitmap); Gdi32.DeleteObject(hBitmap); return(image); }
/// <summary> /// Captures the screen using the SourceCopy | CaptureBlt. /// </summary> /// <param name="height">Height of the capture region.</param> /// <param name="positionX">Source capture Left position.</param> /// <param name="positionY">Source capture Top position.</param> /// <param name="width">Width of the capture region.</param> /// <returns>A bitmap with the capture rectangle.</returns> public static Image CaptureScreenAsBitmap(int width, int height, int positionX, int positionY) { var hDesk = User32.GetDesktopWindow(); var hSrce = User32.GetWindowDC(hDesk); var hDest = Gdi32.CreateCompatibleDC(hSrce); var hBmp = Gdi32.CreateCompatibleBitmap(hSrce, width, height); var hOldBmp = Gdi32.SelectObject(hDest, hBmp); try { var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, positionX, positionY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt); return(b ? Image.FromHbitmap(hBmp) : null); } catch (Exception) { //LogWriter.Log(ex, "Impossible to get screenshot of the screen"); } finally { Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); User32.ReleaseDC(hDesk, hSrce); } return(null); }
public static Image CaptureWindow(IntPtr handle, double scale) { var rectangle = Windows.GetWindowRect(handle); var posX = (int)((rectangle.X + Util.Constants.LeftOffset) * scale); var posY = (int)((rectangle.Y + Util.Constants.TopOffset) * scale); var width = (int)((rectangle.Width - Util.Constants.HorizontalOffset) * scale); var height = (int)((rectangle.Height - Util.Constants.VerticalOffset) * scale); var hDesk = User32.GetDesktopWindow(); var hSrce = User32.GetWindowDC(hDesk); var hDest = Gdi32.CreateCompatibleDC(hSrce); var hBmp = Gdi32.CreateCompatibleBitmap(hSrce, width, height); var hOldBmp = Gdi32.SelectObject(hDest, hBmp); var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, posX, posY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt); try { return(Image.FromHbitmap(hBmp)); } catch (Exception ex) { LogWriter.Log(ex, "Impossible to get screenshot of the screen"); } finally { Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); User32.ReleaseDC(hDesk, hSrce); } return(null); }
/// <summary> /// Captures the screen using the SourceCopy | CaptureBlt. /// </summary> /// <param name="width">The size of the final image.</param> /// <param name="height">The size of the final image.</param> /// <param name="positionX">Source capture Left position.</param> /// <param name="positionY">Source capture Top position.</param> /// <returns>A bitmap with the capture rectangle.</returns> public static BitmapSource CaptureScreenAsBitmapSource(int width, int height, int positionX, int positionY) { var hDesk = User32.GetDesktopWindow(); var hSrce = User32.GetWindowDC(hDesk); var hDest = Gdi32.CreateCompatibleDC(hSrce); var hBmp = Gdi32.CreateCompatibleBitmap(hSrce, width, height); var hOldBmp = Gdi32.SelectObject(hDest, hBmp); try { var b = Gdi32.BitBlt(hDest, 0, 0, width, height, hSrce, positionX, positionY, CopyPixelOperations.SourceCopy | CopyPixelOperations.CaptureBlt); //return Image.FromHbitmap(hBmp); return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBmp, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } catch (Exception ex) { LogWriter.Log(ex, "Impossible to get screenshot of the screen"); } finally { Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); User32.ReleaseDC(hDesk, hSrce); } return(null); }
public Bitmap Capture() { if (GetWindowThreadProcessId() == 0) { return(new Bitmap(1, 1, PixelFormat.Format24bppRgb)); } var deviceSrc = User32.GetWindowDC(this.windowHandle); User32.GetWindowRect(this.windowHandle, ref this.rect); var deviceDest = Gdi32.CreateCompatibleDC(deviceSrc); var bitmapHandle = Gdi32.CreateCompatibleBitmap( deviceSrc, this.rect.Width, this.rect.Height); var oldHandle = Gdi32.SelectObject(deviceDest, bitmapHandle); Gdi32.BitBlt(deviceDest, 0, 0, this.rect.Width, this.rect.Height, deviceSrc, 0, 0, 0x00CC0020); Gdi32.SelectObject(deviceDest, oldHandle); Gdi32.DeleteDC(deviceDest); User32.ReleaseDC(this.windowHandle, deviceSrc); var img = Image.FromHbitmap(bitmapHandle); Gdi32.DeleteObject(bitmapHandle); return(img); }
private static Image CaptureWindow(IntPtr handler) { IntPtr hdcScr = User32.GetWindowDC(handler); var windowRect = new User32.RECT(); User32.GetWindowRect(handler, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; var hdcDesc = Gdi32.CreateCompatibleDC(hdcScr); var hBitmap = Gdi32.CreateCompatibleBitmap(hdcScr, width, height); var hOld = Gdi32.SelectObject(hdcDesc, hBitmap); Gdi32.BitBlt(hdcDesc, 0, 0, width, height, hdcScr, 0, 0, Gdi32.SRCCOPY); Gdi32.SelectObject(hdcDesc, hOld); Gdi32.DeleteDC(hdcDesc); User32.ReleaseDC(handler, hdcScr); var img = Image.FromHbitmap(hBitmap); Gdi32.DeleteObject(hBitmap); return(img); }
/// <summary> /// Captures a Specific Window. /// </summary> /// <param name="Window">The <see cref="Window"/> to Capture</param> /// <param name="IncludeCursor">Whether to include the Mouse Cursor.</param> /// <returns>The Captured Image.</returns> public static Bitmap Capture(Window Window, bool IncludeCursor = false) { User32.GetWindowRect(Window.Handle, out var r); var region = r.ToRectangle(); IntPtr hSrc = GetWindowDC(Window.Handle), hDest = Gdi32.CreateCompatibleDC(hSrc), hBmp = Gdi32.CreateCompatibleBitmap(hSrc, region.Width, region.Height), hOldBmp = Gdi32.SelectObject(hDest, hBmp); Gdi32.BitBlt(hDest, 0, 0, region.Width, region.Height, hSrc, region.Left, region.Top, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); var bmp = Image.FromHbitmap(hBmp); Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); Gdi32.DeleteDC(hSrc); var clone = bmp.Clone(new Rectangle(Point.Empty, bmp.Size), PixelFormat.Format24bppRgb); if (IncludeCursor) { new MouseCursor().Draw(clone, region.Location); } return(clone); }
private Gdi32.HBITMAP GetCompatibleBitmap(Bitmap bm) { using var screenDC = User32.GetDcScope.ScreenDC; // GDI+ returns a DIBSECTION based HBITMAP. The clipboard deals well // only with bitmaps created using CreateCompatibleBitmap(). So, we // convert the DIBSECTION into a compatible bitmap. Gdi32.HBITMAP hBitmap = bm.GetHBITMAP(); // Create a compatible DC to render the source bitmap. using var sourceDC = new Gdi32.CreateDcScope(screenDC); using var sourceBitmapSelection = new Gdi32.SelectObjectScope(sourceDC, hBitmap); // Create a compatible DC and a new compatible bitmap. using var destinationDC = new Gdi32.CreateDcScope(screenDC); Gdi32.HBITMAP bitmap = Gdi32.CreateCompatibleBitmap(screenDC, bm.Size.Width, bm.Size.Height); // Select the new bitmap into a compatible DC and render the blt the original bitmap. using var destinationBitmapSelection = new Gdi32.SelectObjectScope(destinationDC, bitmap); Gdi32.BitBlt( destinationDC, 0, 0, bm.Size.Width, bm.Size.Height, sourceDC, 0, 0, Gdi32.ROP.SRCCOPY); return(bitmap); }
static Bitmap CaptureRegionUnmanaged(Rectangle Region, bool IncludeCursor = false) { IntPtr hSrc = Gdi32.CreateDC("DISPLAY", null, null, 0), hDest = Gdi32.CreateCompatibleDC(hSrc), hBmp = Gdi32.CreateCompatibleBitmap(hSrc, Region.Width, Region.Height), hOldBmp = Gdi32.SelectObject(hDest, hBmp); Gdi32.BitBlt(hDest, 0, 0, Region.Width, Region.Height, hSrc, Region.Left, Region.Top, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); var bmp = Image.FromHbitmap(hBmp); Gdi32.SelectObject(hDest, hOldBmp); Gdi32.DeleteObject(hBmp); Gdi32.DeleteDC(hDest); Gdi32.DeleteDC(hSrc); var clone = bmp.Clone(new Rectangle(Point.Empty, bmp.Size), PixelFormat.Format24bppRgb); if (IncludeCursor) { new MouseCursor().Draw(clone, Region.Location); } return(clone); }
// 特定窗口的截图对象 private Image CaptureWindow(IntPtr handle) { // 获得目标窗口的hDC SafeDCHandle hdcSrc = User32.GetWindowDC(handle); var screenSize = GetScreenPhysicalSzie(); // create a device context we can copy to var hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, screenSize.Width, screenSize.Height); // select the bitmap object IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap); // bitblt over Gdi32.BitBlt(hdcDest.HWnd, 0, 0, screenSize.Width, screenSize.Height, hdcSrc.HWnd, 0, 0, WindowsAPIUtils.SRCCOPY); // restore selection Gdi32.SelectObject(hdcDest, hOld); // clean up Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc.HWnd); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object Gdi32.DeleteObject(hBitmap); return(img); }
// this DC is cached and should only be deleted on Dispose or when the size changes. public Gdi32.HDC GetCachedItemDC(Gdi32.HDC toolStripHDC, Size bitmapSize) { if (_cachedHDCSize.Width < bitmapSize.Width || _cachedHDCSize.Height < bitmapSize.Height) { if (_cachedItemHDC.IsNull) { // Create a new DC - we don't have one yet. _cachedItemHDC = Gdi32.CreateCompatibleDC(toolStripHDC); } // Create compatible bitmap with the correct size. _cachedItemBitmap = Gdi32.CreateCompatibleBitmap(toolStripHDC, bitmapSize.Width, bitmapSize.Height); Gdi32.HGDIOBJ oldBitmap = Gdi32.SelectObject(_cachedItemHDC, _cachedItemBitmap); // Delete the old bitmap if (!oldBitmap.IsNull) { Gdi32.DeleteObject(oldBitmap); } // remember what size we created. _cachedHDCSize = bitmapSize; } return(_cachedItemHDC); }
private void SetSpotInfo() { int x = _originX; int y = _originY; IntPtr hwnd = User32.GetDesktopWindow(); IntPtr dc = User32.GetWindowDC(hwnd); IntPtr memDC = Gdi32.CreateCompatibleDC(dc); IntPtr hbm = Gdi32.CreateCompatibleBitmap(dc, _spotWidth, _spotHeight); IntPtr oldbm = Gdi32.SelectObject(memDC, hbm); Gdi32.BitBlt( memDC, 0, 0, this.SpotWidth, this.SpotHeight, dc, x, y, 0x40CC0020 ); _currentSpotBmp = Image.FromHbitmap(hbm); Gdi32.SelectObject(memDC, oldbm); Gdi32.DeleteObject(hbm); Gdi32.DeleteDC(memDC); User32.ReleaseDC(hwnd, dc); }
public RegionProvider(Rectangle region, bool includeCursor, Func <Point> locationFunc) { _region = region; _includeCursor = includeCursor; _locationFunc = locationFunc; // Width and Height must be even. // Use these for Bitmap size, but capture as per region size Width = _region.Width; if (Width % 2 == 1) { ++Width; } Height = _region.Height; if (Height % 2 == 1) { ++Height; } _transform = point => new Point(point.X - _region.X, point.Y - _region.Y); _hdcSrc = User32.GetDC(IntPtr.Zero); _hdcDest = Gdi32.CreateCompatibleDC(_hdcSrc); _hBitmap = Gdi32.CreateCompatibleBitmap(_hdcSrc, Width, Height); Gdi32.SelectObject(_hdcDest, _hBitmap); }
private void FromWindow(IntPtr hwnd) { IntPtr hDc = User32.GetDC(hwnd); IntPtr memhDC = Gdi32.CreateCompatibleDC(hDc); User32.RECT clientSize; User32.GetClientRect(hwnd, out clientSize); Point bounds = new Point(clientSize.Right - clientSize.Left, clientSize.Bottom - clientSize.Top); IntPtr hBmp = Gdi32.CreateCompatibleBitmap(hDc, bounds.X, bounds.Y); Gdi32.SelectObject(memhDC, hBmp); Gdi32.BitBlt(memhDC, 0, 0, bounds.X, bounds.Y, hDc, 0, 0, Gdi32.TernaryRasterOperations.SRCCOPY); Bitmap bmp = Bitmap.FromHbitmap(hBmp); Gdi32.DeleteObject(hBmp); User32.ReleaseDC(hwnd, hDc); Gdi32.DeleteDC(memhDC); FromBitmap(bmp); }
public GdiTargetDeviceContext(IntPtr SrcDc, int Width, int Height) { _hdcDest = Gdi32.CreateCompatibleDC(SrcDc); _hBitmap = Gdi32.CreateCompatibleBitmap(SrcDc, Width, Height); Gdi32.SelectObject(_hdcDest, _hBitmap); }
/// <summary> /// Windows only window capture by handle /// </summary> /// <param name="handle"></param> /// <returns></returns> public static byte[] WindowsCapturePng(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); byte[] ret = HbitmapToPng(hBitmap); // free up the Bitmap object Gdi32.DeleteObject(hBitmap); return(ret); }
public Bitmap 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 image = Image.FromHbitmap(hBitmap); Gdi32.DeleteObject(hBitmap); return(new Bitmap(image)); }
private Image CaptureWindow(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size RECT windowRect = new RECT(); User32.GetWindowRect(handle, 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); 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); }
/// <summary> /// Creates an Image object containing a screen shot of a specific window /// </summary> /// <param name="handle">A <see cref="IntPtr"/> to the window to capture. /// (In windows forms, this is obtained by the Handle property)</param> /// <returns>An <see cref="Image"/> with the captured window if successfull, /// otherwise null.</returns> /// <remarks>This method uses the GDI32.BitBlt() /// method, so the window has to be visible and should not be /// minimized or hidden for example. In this cases use /// <see cref="GetWindowImage(IntPtr,Size)"/> which uses /// User32.PrintWindow(). /// </remarks> public static Image CaptureWindow(IntPtr handle) { IntPtr hdcSrc = IntPtr.Zero; IntPtr hdcDest = IntPtr.Zero; IntPtr bitmap = IntPtr.Zero; Image img; try { // get te hDC of the target window 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 hdcDest = Gdi32.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height bitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr old = Gdi32.SelectObject(hdcDest, bitmap); // bitblt over Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, Gdi32.TernaryRasterOperations.SRCCOPY); // restore selection Gdi32.SelectObject(hdcDest, old); // get a .NET image object for it img = Image.FromHbitmap(bitmap); } catch (Exception ex) { VGExceptionMethods.HandleException(ex); return(null); } finally { // clean up Gdi32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); // free up the Bitmap object Gdi32.DeleteObject(bitmap); } return(img); }
// capture a region of a the screen, defined by the hWnd public static BitmapSource CaptureRegion( IntPtr hWnd, int x, int y, int width, int height, bool addToClipboard) { IntPtr sourceDC = IntPtr.Zero; IntPtr targetDC = IntPtr.Zero; IntPtr compatibleBitmapHandle = IntPtr.Zero; BitmapSource bitmap = null; try { // gets the main desktop and all open windows sourceDC = User32.GetDC(User32.GetDesktopWindow()); //sourceDC = User32.GetDC(hWnd); targetDC = Gdi32.CreateCompatibleDC(sourceDC); // create a bitmap compatible with our target DC compatibleBitmapHandle = Gdi32.CreateCompatibleBitmap(sourceDC, width, height); // gets the bitmap into the target device context Gdi32.SelectObject(targetDC, compatibleBitmapHandle); // copy from source to destination Gdi32.BitBlt(targetDC, 0, 0, width, height, sourceDC, x, y, Gdi32.SRCCOPY); // Here's the WPF glue to make it all work. It converts from an // hBitmap to a BitmapSource. Love the WPF interop functions bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( compatibleBitmapHandle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (addToClipboard) { //Clipboard.SetImage(bitmap); // high memory usage for large images IDataObject data = new DataObject(); data.SetData(DataFormats.Dib, bitmap, false); Clipboard.SetDataObject(data, false); } } catch (Exception ex) { throw new ScreenCaptureException(string.Format("Error capturing region {0},{1},{2},{3}", x, y, width, height), ex); } finally { Gdi32.DeleteObject(compatibleBitmapHandle); User32.ReleaseDC(IntPtr.Zero, sourceDC); User32.ReleaseDC(IntPtr.Zero, targetDC); } return(bitmap); }
/* * InitBackBuffer */ /// <summary> /// Creates the offscreen DC and associated bitmap. /// </summary> /// <param name="dc">Specifies drawing context for this <see cref="T:Genetibase.UI.NuGenPushGraphBar"/>.</param> /// <returns></returns> protected virtual IntPtr InitBackBuffer(IntPtr dc) { this.dcBack = Gdi32.CreateCompatibleDC(dc); Rectangle clientRectangle = this.ClientRectangle; this.bmBack = Gdi32.CreateCompatibleBitmap( dc, this.ClientRectangle.Width, this.ClientRectangle.Height ); Gdi32.SelectObject(this.dcBack, this.bmBack); return(dcBack); }