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); }
/// <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> /// <param name="height">The height.</param> /// <param name="width">The width.</param> /// <param name="nXDest">The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.</param> /// <param name="nYDest">The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.</param> /// <param name="nXSrc">The x-coordinate, in logical units, of the upper-left corner of the source rectangle.</param> /// <param name="nYSrc">The y-coordinate, in logical units, of the upper-left corner of the source rectangle.</param> /// <returns></returns> public static Image CaptureWindow(IntPtr handle, int height, int width, int nXSrc = 0, int nYSrc = 0, int nXDest = 0, int nYDest = 0) { // 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, nXDest, nYDest, width, height, hdcSrc, nXSrc, nYSrc, 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 = null; try { if (hBitmap != null && hBitmap != IntPtr.Zero) { img = Image.FromHbitmap(hBitmap); } else { } } catch { } finally { // 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">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 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); }
/* Author: Perry Lee * Submission: Capture Screen (Add Screenshot Capability to Programs) * Date of Submission: 12/29/03 */ public static void CaptureWindow(IntPtr hwnd, int winx, int winy, Stream str, ImageFormat imageFormat) { int hdcSrc = 0; Graphics g = Graphics.FromHwnd(hwnd); hdcSrc = (int)g.GetHdc(); if (hdcSrc != 0) { int 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, winx, winy, 0x00CC0020); SaveImageAs(hBitmap, str, imageFormat); Cleanup(hBitmap, hdcSrc, hdcDest); } if (g != null) { g.Dispose(); } }
/// <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 // double factor = System.Windows.PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11; int screenWidth = Screen.PrimaryScreen.Bounds.Width; int screenHeight = Screen.PrimaryScreen.Bounds.Height; 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, screenWidth, screenHeight); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, screenWidth, screenHeight, 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); }
// Takes a screenshot of the SnapRegion and returns it private Image Snap() { bool VfwWasVisible = vfw.Visible; if (VfwWasVisible) { vfw.Visible = false; // Hide viewfinder if appropriate } this.Visible = false; // Hide self (nobody wants to translate Babel) try { if ((int)TrackingWindow != 0) { // Put this into a general function later Rectangle WindowLoc = WindowFunctions.GetRectFromHwnd(TrackingWindow); vfw.Location = new Point(WindowLoc.Left, WindowLoc.Top); vfw.Size = new Size(WindowLoc.Width, WindowLoc.Height); vfw.Flicker(); } Image result = GDI32.Grab(SnapRegion); if (VfwWasVisible) { vfw.Visible = true; // Reshow viewfinder if appropriate } this.Visible = true; // Show self again this.Focus(); // Return focus to the main form DebugLog.Log("Took snap"); return(result); } catch (Exception ex) { DebugLog.Log(ex.Message); return(null); } }
/// <summary> /// Captures the desktop /// </summary> /// <returns>The bitmap of the desktop</returns> public static Bitmap Capture(bool withCursor) { int cursorX = 0, cursorY = 0; Graphics g; Rectangle r; int width = 0; foreach (Screen scr in Screen.AllScreens) { width += scr.Bounds.Width; } int height = Screen.PrimaryScreen.Bounds.Height; //width and height also = GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10) int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()), hdcDest = GDI32.CreateCompatibleDC(hdcSrc), hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 0x00CC0020); //GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10) Bitmap image = Image.FromHbitmap(new IntPtr(hBitmap)); Bitmap cursor = null; if (withCursor) { cursor = CaptureCursor(ref cursorX, ref cursorY); } Cleanup(hBitmap, hdcSrc, hdcDest); if (cursor != null) { r = new Rectangle(cursorX, cursorY, cursor.Width, cursor.Height); g = Graphics.FromImage(image); g.DrawImage(cursor, r); g.Flush(); } //image.Save(@"c:\image"); return(image); }
static IntPtr getDeviceContext(IntPtr whndl) { if (_lastSrcHndl == whndl) { if (_lastSrcDC == IntPtr.Zero) { _lastSrcDC = User32.GetWindowDC(whndl); } } else { if (_lastSrcDC != IntPtr.Zero) { User32.ReleaseDC(_lastSrcHndl, _lastSrcDC); GDI32.DeleteObject(_lastSrcDC); _lastSrcDC = IntPtr.Zero; } _lastSrcDC = User32.GetWindowDC(whndl); _lastSrcHndl = whndl; } return(_lastSrcDC); }
private IntPtr CaptureCachedPointer(IntPtr handle) { // get te hDC of the target window 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); return(hBitmap); }
/// <summary> /// Gets a segment of the desktop as an image. /// </summary> /// <returns>A <see cref="System.Drawing.Image" /> containg an image of the full desktop.</returns> public Image GetWindowBitmap(IntPtr hWnd) { var windowRect = new User32.RECT(); User32.GetWindowRect(hWnd, ref windowRect); var width = windowRect.Right - windowRect.Left; var height = windowRect.Bottom - windowRect.Top; Image destinationImage = new Bitmap(width, height, PixelFormat.Format32bppRgb); using (var destinationGraphics = Graphics.FromImage(destinationImage)) { var destinationGraphicsHandle = IntPtr.Zero; var windowDC = IntPtr.Zero; try { //Pointers for window handles destinationGraphicsHandle = destinationGraphics.GetHdc(); windowDC = User32.GetWindowDC(hWnd); //Get the screencapture var dwRop = GDI32.TernaryRasterOperations.SRCCOPY | GDI32.TernaryRasterOperations.CAPTUREBLT; User32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, User32.RedrawWindowFlags.InternalPaint); GDI32.BitBlt(destinationGraphicsHandle, 0, 0, width, height, windowDC, 0, 0, dwRop); } finally { destinationGraphics.ReleaseHdc(destinationGraphicsHandle); GDI32.DeleteDC(destinationGraphicsHandle); // User32.ReleaseDC(windowDC) GDI32.DeleteDC(windowDC); } } // Don't forget to dispose this image return(destinationImage); }
/// <summary> /// ScreenShot'ın BitmapSource'unu verir. /// </summary> /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param> /// <returns></returns> public static System.Windows.Media.Imaging.BitmapSource CaptureWindowAsBitmapSource(IntPtr handle, Rectangle bounds) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT() { left = bounds.Left, top = bounds.Top, bottom = bounds.Top + bounds.Height, right = bounds.Left + bounds.Width }; User32.GetWindowRect(handle, ref windowRect); // 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, bounds.Width, bounds.Height); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, bounds.Width, bounds.Height, hdcSrc, bounds.Left, bounds.Top, GDI32.SRCCOPY); // restore selection GDI32.SelectObject(hdcDest, hOld); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); System.Windows.Media.Imaging.BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromWidthAndHeight(bounds.Width, bounds.Height)); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(bitmapSource); }
public BitmapSource CaptureWindowBmSrc(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); BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); //freeze bitmapSource and clear memory to avoid memory leaks bitmapSource.Freeze(); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); return(bitmapSource); }
public static Image CaptureWindow(IntPtr handle, Rectangle r) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size User32.RECT windowRect = new User32.RECT() { left = r.Left, top = r.Top, right = r.Right, bottom = r.Bottom }; //User32.GetWindowRect(handle, ref windowRect); int width = r.Width; int height = r.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, r.Left, r.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); }
/// <summary> /// Gets a segment of the desktop as an image. /// </summary> /// <returns>A <see cref="System.Drawing.Image" /> containg an image of the full desktop.</returns> public Image GetDesktopBitmapBg(IntPtr hWnd, bool forcePrintWindow = false, bool forceBitBlt = false) { var rect = new User32.RECT(); User32.GetClientRect(hWnd, ref rect); bool dwmEnabled; DWM.DwmIsCompositionEnabled(out dwmEnabled); if ((!dwmEnabled && !forcePrintWindow) || forceBitBlt) { return(GetDesktopBitmap(hWnd, rect)); } var img = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppRgb); using (var g = Graphics.FromImage(img)) { var dc = g.GetHdc(); // User32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, User32.RedrawWindowFlags.Frame | User32.RedrawWindowFlags.Invalidate | User32.RedrawWindowFlags.Erase | User32.RedrawWindowFlags.UpdateNow | User32.RedrawWindowFlags.AllChildren); var success = User32.PrintWindow(hWnd, dc, 1); g.ReleaseHdc(dc); GDI32.DeleteDC(dc); if (!success && !forcePrintWindow) { return(GetDesktopBitmap(hWnd, rect)); } if (!forcePrintWindow && img.Width > 64 && img.Height > 64 && img.IsAllBlack()) { return(GetDesktopBitmap(hWnd, rect)); } } return(img); }
public static Image GetSingleFrame() { var handle = User32.GetDesktopWindow(); // 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); IntPtr hBitmap; IntPtr hOld; // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object hOld = GDI32.SelectObject(hdcDest, hBitmap); // select the bitmap object 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); // get a .NET image object for it var screen = Image.FromHbitmap(hBitmap); // clean up // free up the Bitmap object GDI32.DeleteObject(hBitmap); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); GC.Collect(); return(screen); }
/// <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 Image CaptureWindow(IntPtr handle) { // get te hDC of the target window IntPtr hdcSrc = User32.GetWindowDC(handle); // get the size Rectangle rect = new Rectangle(); User32.GetWindowRect(handle, out rect); int width = rect.Width - rect.X; int height = rect.Height - rect.Y; // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // create a device context we can copy to IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // select the bitmap object IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // bitblt over GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); //Print window User32.PrintWindow(handle, hdcDest, 0); // restore selection GDI32.SelectObject(hdcDest, hOld); // get a .NET image object for it Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); // clean up GDI32.DeleteDC(hdcDest); User32.ReleaseDC(handle, hdcSrc); return(img); }
/// <summary> /// Capture a screen shot of a specific window. /// </summary> /// <param name="handle">The window handle.</param> /// <remarks>The window <c>handle</c>is obtained using the <c>Handle</c>property of the <c>Form</c> class.</remarks> /// <returns>The window as an <c>Image</c> object.</returns> public Image CaptureWindow(IntPtr handle) { // Get the device context of the source window. IntPtr hdcSrc = User32.GetWindowDC(handle); // Get the size of the window. User32.RECT windowRect = new User32.RECT(); User32.GetWindowRect(handle, ref windowRect); int width = windowRect.right - windowRect.left; int height = windowRect.bottom - windowRect.top; // Create device context for the destination. IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); // Create a bitmap of the source window. IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); // Select the bitmap object. IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); // Bit Block Transfer - BITBLT the data. 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); // Create an Image object corresponding to the bitmap. Image image = Image.FromHbitmap(hBitmap); // Free up the bitmap object. GDI32.DeleteObject(hBitmap); return(image); }
/// <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 (aviWriter != null) { aviWriter.Dispose(); aviWriter = null; } }
protected override void WndProc(ref Message m) { if (capturing) { switch (m.Msg) { case (int)QAliber.Recorder.Structures.MouseMessages.WM_LBUTTONUP: User32.ReleaseCapture(); GDI32.RedrawWindow(capturedElement); Cursor = Cursors.Default; toolStripCapture.Image = Resources.Crosshair; hotkey_HotkeyPressed(this, EventArgs.Empty); capturing = false; break; case (int)QAliber.Recorder.Structures.MouseMessages.WM_MOUSEMOVE: try { AutomationElement element = AutomationElement.FromPoint(new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y)); if (!element.Equals(capturedElement)) { GDI32.RedrawWindow(capturedElement); GDI32.HighlightWindow(element); capturedElement = element; } } catch { } break; default: break; } } base.WndProc(ref m); }
public Image CaptureWindow(IntPtr handle, Rectangle area) { // 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 = (int)((windowRect.right - windowRect.left) * scalingFactor); // int height = (int)((windowRect.bottom - windowRect.top) * scalingFactor); int x = (int)(area.Left * ScalingFactor); int y = (int)(area.Top * ScalingFactor); int width = (int)(area.Width * ScalingFactor); int height = (int)(area.Height * ScalingFactor); // 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 Image img = Image.FromHbitmap(hBitmap); // free up the Bitmap object GDI32.DeleteObject(hBitmap); return(img); }
public Image GetBitmapFromGameWindow() { var gameWindow = _windowHandler.GetGameWindow(); if (gameWindow == IntPtr.Zero) { return(null); } var gameWindowDc = _windowHandler.GetGameWindowDC(); if (gameWindowDc == IntPtr.Zero) { _windowHandler.ResetWindow(); return(null); } var windowRect = _windowHandler.GetWindowRect(); int width = windowRect.Right - windowRect.Left; int height = windowRect.Bottom - windowRect.Top; IntPtr hdcDest = GDI32.CreateCompatibleDC(gameWindowDc); IntPtr hBitmap = GDI32.CreateCompatibleBitmap(gameWindowDc, width, height); IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); GDI32.BitBlt(hdcDest, 0, 0, width, height, gameWindowDc, 0, 0, GDI32.SRCCOPY); GDI32.SelectObject(hdcDest, hOld); GDI32.DeleteDC(hdcDest); User32.ReleaseDC(gameWindow, gameWindowDc); Image image = Image.FromHbitmap(hBitmap); GDI32.DeleteObject(hBitmap); return(image); }
/// <summary> /// プライマリスクリーンの画像を取得する /// </summary> /// <returns>プライマリスクリーンの画像</returns> public static Bitmap CaptureRect(Rectangle rc) { //プライマリモニタのデバイスコンテキストを取得 IntPtr hsrcDC = User32.GetDC(IntPtr.Zero); //Bitmapの作成 Bitmap bmp = new Bitmap(rc.Width, rc.Height); //Graphicsの作成 Graphics g = Graphics.FromImage(bmp); //Graphicsのデバイスコンテキストを取得 IntPtr hdestDC = g.GetHdc(); //Bitmapに画像をコピーする GDI32.BitBlt(hdestDC, 0, 0, bmp.Width, bmp.Height, hsrcDC, rc.Left, rc.Top, GDI32.SRCCOPY | GDI32.CAPTUREBLT); //解放 g.ReleaseHdc(hdestDC); g.Dispose(); User32.ReleaseDC(IntPtr.Zero, hsrcDC); return(bmp); }
private async Task SendFrame() { IntPtr hWnd = IntPtr.Zero; IntPtr hDC = IntPtr.Zero; IntPtr graphDC = IntPtr.Zero; try { hWnd = User32.GetDesktopWindow(); hDC = User32.GetWindowDC(hWnd); graphDC = Graphic.GetHdc(); var copyResult = GDI32.BitBlt(graphDC, 0, 0, screenShot.TotalWidth, screenShot.TotalHeight, hDC, 0 + offsetX, 0 + offsetY, GDI32.TernaryRasterOperations.SRCCOPY | GDI32.TernaryRasterOperations.CAPTUREBLT); if (!copyResult) { Graphic.ReleaseHdc(graphDC); Graphic.Clear(System.Drawing.Color.White); var font = new Font(System.Drawing.FontFamily.GenericSansSerif, 30, System.Drawing.FontStyle.Bold); Graphic.DrawString("Waiting for screen capture...", font, System.Drawing.Brushes.Black, new PointF((screenShot.TotalWidth / 2), screenShot.TotalHeight / 2), new StringFormat() { Alignment = StringAlignment.Center }); } else { Graphic.ReleaseHdc(graphDC); User32.ReleaseDC(hWnd, hDC); } // Get cursor information to draw on the screenshot. var ci = new User32.CursorInfo(); ci.cbSize = Marshal.SizeOf(ci); User32.GetCursorInfo(out ci); if (ci.flags == User32.CURSOR_SHOWING) { using (var icon = System.Drawing.Icon.FromHandle(ci.hCursor)) { Graphic.DrawIcon(icon, ci.ptScreenPos.x, ci.ptScreenPos.y); } } if (sendFullScreenshot) { var request = new { Type = "Bounds", Width = screenShot.TotalWidth, Height = screenShot.TotalHeight }; await SocketSend(request); using (var ms = new MemoryStream()) { screenShot.CurrentFrame.Save(ms, ImageFormat.Jpeg); ms.WriteByte(0); ms.WriteByte(0); ms.WriteByte(0); ms.WriteByte(0); ms.WriteByte(0); ms.WriteByte(0); await Socket.SendAsync(new ArraySegment <byte>(ms.ToArray()), WebSocketMessageType.Binary, true, CancellationToken.None); sendFullScreenshot = false; return; } } NewData = screenShot.GetNewData(); if (NewData == null) { await Task.Delay(100); // Ignore async warning here since it's intentional. This is to prevent deadlock. #pragma warning disable SendFrame(); #pragma warning restore } else { using (var ms = new MemoryStream()) { screenShot.SaveCroppedFrame(ms); // Add x,y coordinates of top-left of image so receiver knows where to draw it. foreach (var metaByte in NewData) { ms.WriteByte(metaByte); } await Socket.SendAsync(new ArraySegment <byte>(ms.ToArray()), WebSocketMessageType.Binary, true, CancellationToken.None); } } screenShot.CloneLastFrame(); } catch (Exception ex) { WriteToLog(ex); if (graphDC != IntPtr.Zero) { Graphic.ReleaseHdc(graphDC); } if (hDC != IntPtr.Zero) { User32.ReleaseDC(hWnd, hDC); } capturing = false; stackMain.Visibility = Visibility.Collapsed; stackReconnect.Visibility = Visibility.Visible; textAgentStatus.FontWeight = FontWeights.Normal; textAgentStatus.Foreground = new SolidColorBrush(Colors.Black); textAgentStatus.Text = "Not Connected"; } }
private MemoryStream SaveBitmapToStream(IntPtr pBits) { MemoryStream memoryStream = new MemoryStream(); IntPtr hdc = IntPtr.Zero; IntPtr hPalette = IntPtr.Zero; try { hdc = User32.GetDC(IntPtr.Zero); if (hdc == IntPtr.Zero) { throw new ExternalException("Call to native API (GetDC) failed"); } // Compute bitmap size and create it if needed int paletteSize = 0; IntPtr structBmpInfoPtr = new IntPtr(pBits.ToInt64() + Marshal.OffsetOf(typeof(GDI32.BITMAPINFOHEADER), "biSize").ToInt64()); int structBmpInfoSize = Marshal.ReadInt32(structBmpInfoPtr); IntPtr bitCountPtr = new IntPtr(pBits.ToInt64() + Marshal.OffsetOf(typeof(GDI32.BITMAPINFOHEADER), "biBitCount").ToInt64()); int bitCount = Marshal.ReadInt32(bitCountPtr); if (bitCount <= 8) { paletteSize = 1 << bitCount; IntPtr colorUsedPtr = new IntPtr(pBits.ToInt64() + Marshal.OffsetOf(typeof(GDI32.BITMAPINFOHEADER), "biClrUsed").ToInt64()); int colorUsed = Marshal.ReadInt32(colorUsedPtr); if (colorUsed > 0) { paletteSize = colorUsed; } paletteSize *= 4; // 4 -> sizeof(RGBQUAD) // Create palette IntPtr palettePtr = IntPtr.Zero; try { int structLogPaletteSize = Marshal.SizeOf(typeof(GDI32.LOGPALETTE)); palettePtr = Marshal.AllocHGlobal(structLogPaletteSize + paletteSize); IntPtr palVersion = new IntPtr(palettePtr.ToInt64() + Marshal.OffsetOf(typeof(GDI32.LOGPALETTE), "palVersion").ToInt64()); IntPtr palNumEntries = new IntPtr(palettePtr.ToInt64() + Marshal.OffsetOf(typeof(GDI32.LOGPALETTE), "palNumEntries").ToInt64()); Marshal.WriteInt16(palVersion, 0x300); Marshal.WriteInt16(palNumEntries, (short)(paletteSize / 4)); for (int t = 0; t < paletteSize; t += 4) { IntPtr pRgbfBits = new IntPtr(pBits.ToInt64() + structBmpInfoSize + t); IntPtr pRgbfPallette = new IntPtr(palettePtr.ToInt64() + structLogPaletteSize + t); Marshal.WriteInt32(pRgbfPallette, Marshal.ReadInt32(pRgbfBits)); // } hPalette = GDI32.CreatePalette(palettePtr); if (hPalette == IntPtr.Zero) { throw new ExternalException("Call to native API (CreatePalette) failed"); } } finally { if (palettePtr != IntPtr.Zero) { Marshal.FreeHGlobal(palettePtr); palettePtr = IntPtr.Zero; } } } // Create hBitmap, create Bitmap from it and save as stream IntPtr offsetToBits = new IntPtr(pBits.ToInt64() + structBmpInfoSize + paletteSize); IntPtr hBitmap = GDI32.CreateDIBitmap(hdc, pBits, GDI32.CBM_INIT, offsetToBits, pBits, GDI32.DIB_RGB_COLORS); if (hBitmap == IntPtr.Zero) { throw new ExternalException("Call to native API (CreateDIBitmap) failed"); } using (Bitmap bmp = (hPalette == IntPtr.Zero) ? Bitmap.FromHbitmap(hBitmap) : Bitmap.FromHbitmap(hBitmap, hPalette)) { bmp.Save(memoryStream, ImageFormat.Png); } // } finally { if (hdc != IntPtr.Zero) { User32.ReleaseDC(IntPtr.Zero, hdc); hdc = IntPtr.Zero; } if (hPalette != IntPtr.Zero) { GDI32.DeleteObject(hPalette); hPalette = IntPtr.Zero; } } return(memoryStream); }
public void Start(int framesPerSecond) { string filename; if (recordingWindow != null) { string windowTitle = Regex.Replace(recordingWindow.Text, @"[^\x20\d\w]", ""); if (string.IsNullOrEmpty(windowTitle)) { windowTitle = "greenshot-recording"; } filename = Path.Combine(conf.OutputFilePath, windowTitle + ".avi"); } else { filename = Path.Combine(conf.OutputFilePath, "greenshot-recording.avi"); } if (File.Exists(filename)) { try { File.Delete(filename); } catch {} } LOG.InfoFormat("Capturing to {0}", filename); if (recordingWindow != null) { LOG.InfoFormat("Starting recording Window '{0}', {1}", recordingWindow.Text, recordingWindow.ClientRectangle); recordingSize = recordingWindow.ClientRectangle.Size; } else { LOG.InfoFormat("Starting recording rectangle {0}", recordingRectangle); recordingSize = recordingRectangle.Size; } if (recordingSize.Width % 8 > 0) { LOG.InfoFormat("Correcting width to be factor 8, {0} => {1}", recordingSize.Width, recordingSize.Width + (8 - (recordingSize.Width % 8))); recordingSize = new Size(recordingSize.Width + (8 - (recordingSize.Width % 8)), recordingSize.Height); } if (recordingSize.Height % 8 > 0) { LOG.InfoFormat("Correcting Height to be factor 8, {0} => {1}", recordingSize.Height, recordingSize.Height + (8 - (recordingSize.Height % 8))); recordingSize = new Size(recordingSize.Width, recordingSize.Height + (8 - (recordingSize.Height % 8))); } this.framesPerSecond = framesPerSecond; // "P/Invoke" Solution for capturing the screen hWndDesktop = User32.GetDesktopWindow(); // get te hDC of the target window hDCDesktop = User32.GetWindowDC(hWndDesktop); // Make sure the last error is set to 0 Win32.SetLastError(0); // create a device context we can copy to hDCDest = GDI32.CreateCompatibleDC(hDCDesktop); // Check if the device context is there, if not throw an error with as much info as possible! if (hDCDest == IntPtr.Zero) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateCompatibleDC", recordingSize); // Cleanup User32.ReleaseDC(hWndDesktop, hDCDesktop); // throw exception throw exceptionToThrow; } // Create BitmapInfoHeader for CreateDIBSection BitmapInfoHeader bitmapInfoHeader = new BitmapInfoHeader(recordingSize.Width, recordingSize.Height, 32); // Make sure the last error is set to 0 Win32.SetLastError(0); // create a bitmap we can copy it to, using GetDeviceCaps to get the width/height hDIBSection = GDI32.CreateDIBSection(hDCDesktop, ref bitmapInfoHeader, BitmapInfoHeader.DIB_RGB_COLORS, out bits0, IntPtr.Zero, 0); if (hDIBSection == IntPtr.Zero) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateDIBSection", recordingSize); exceptionToThrow.Data.Add("hdcDest", hDCDest.ToInt32()); exceptionToThrow.Data.Add("hdcSrc", hDCDesktop.ToInt32()); // clean up GDI32.DeleteDC(hDCDest); User32.ReleaseDC(hWndDesktop, hDCDesktop); // Throw so people can report the problem throw exceptionToThrow; } // Create a GDI Bitmap so we can use GDI and GDI+ operations on the same memory GDIBitmap = new Bitmap(recordingSize.Width, recordingSize.Height, 32, PixelFormat.Format32bppArgb, bits0); // select the bitmap object and store the old handle hOldObject = GDI32.SelectObject(hDCDest, hDIBSection); stop = false; aviWriter = new AVIWriter(); // Comment the following 2 lines to make the user select it's own codec //aviWriter.Codec = "msvc"; //aviWriter.Quality = 99; aviWriter.FrameRate = framesPerSecond; aviWriter.Open(filename, recordingSize.Width, recordingSize.Height); // Start update check in the background backgroundTask = new Thread(new ThreadStart(CaptureFrame)); backgroundTask.IsBackground = true; backgroundTask.Start(); }
/// <summary> /// This method will use User32 code to capture the specified captureBounds from the screen /// </summary> /// <param name="captureBounds">Rectangle with the bounds to capture</param> /// <returns>Bitmap which is captured from the screen at the location specified by the captureBounds</returns> public static Bitmap CaptureRectangle(Rectangle captureBounds) { Bitmap returnBitmap = null; if (captureBounds.Height <= 0 || captureBounds.Width <= 0) { LOG.Warn("Nothing to capture, ignoring!"); return(null); } LOG.Debug("CaptureRectangle Called!"); // .NET GDI+ Solution, according to some post this has a GDI+ leak... // See http://connect.microsoft.com/VisualStudio/feedback/details/344752/gdi-object-leak-when-calling-graphics-copyfromscreen // Bitmap capturedBitmap = new Bitmap(captureBounds.Width, captureBounds.Height); // using (Graphics graphics = Graphics.FromImage(capturedBitmap)) { // graphics.CopyFromScreen(captureBounds.Location, Point.Empty, captureBounds.Size, CopyPixelOperation.CaptureBlt); // } // capture.Image = capturedBitmap; // capture.Location = captureBounds.Location; using (SafeWindowDCHandle desktopDCHandle = SafeWindowDCHandle.fromDesktop()) { if (desktopDCHandle.IsInvalid) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("desktopDCHandle", captureBounds); // throw exception throw exceptionToThrow; } // create a device context we can copy to using (SafeCompatibleDCHandle safeCompatibleDCHandle = GDI32.CreateCompatibleDC(desktopDCHandle)) { // Check if the device context is there, if not throw an error with as much info as possible! if (safeCompatibleDCHandle.IsInvalid) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateCompatibleDC", captureBounds); // throw exception throw exceptionToThrow; } // Create BITMAPINFOHEADER for CreateDIBSection BITMAPINFOHEADER bmi = new BITMAPINFOHEADER(captureBounds.Width, captureBounds.Height, 24); // Make sure the last error is set to 0 Win32.SetLastError(0); // create a bitmap we can copy it to, using GetDeviceCaps to get the width/height IntPtr bits0; // not used for our purposes. It returns a pointer to the raw bits that make up the bitmap. using (SafeDibSectionHandle safeDibSectionHandle = GDI32.CreateDIBSection(desktopDCHandle, ref bmi, BITMAPINFOHEADER.DIB_RGB_COLORS, out bits0, IntPtr.Zero, 0)) { if (safeDibSectionHandle.IsInvalid) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateDIBSection", captureBounds); exceptionToThrow.Data.Add("hdcDest", safeCompatibleDCHandle.DangerousGetHandle().ToInt32()); exceptionToThrow.Data.Add("hdcSrc", desktopDCHandle.DangerousGetHandle().ToInt32()); // Throw so people can report the problem throw exceptionToThrow; } // select the bitmap object and store the old handle using (safeCompatibleDCHandle.SelectObject(safeDibSectionHandle)) { // bitblt over (make copy) GDI32.BitBlt(safeCompatibleDCHandle, 0, 0, captureBounds.Width, captureBounds.Height, desktopDCHandle, captureBounds.X, captureBounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); } // get a .NET image object for it // A suggestion for the "A generic error occurred in GDI+." E_FAIL/0×80004005 error is to re-try... bool success = false; ExternalException exception = null; for (int i = 0; i < 3; i++) { try { // Collect all screens inside this capture List <Screen> screensInsideCapture = new List <Screen>(); foreach (Screen screen in Screen.AllScreens) { if (screen.Bounds.IntersectsWith(captureBounds)) { screensInsideCapture.Add(screen); } } // Check all all screens are of an equal size bool offscreenContent; using (Region captureRegion = new Region(captureBounds)) { // Exclude every visible part foreach (Screen screen in screensInsideCapture) { captureRegion.Exclude(screen.Bounds); } // If the region is not empty, we have "offscreenContent" using (Graphics screenGraphics = Graphics.FromHwnd(User32.GetDesktopWindow())) { offscreenContent = !captureRegion.IsEmpty(screenGraphics); } } // Check if we need to have a transparent background, needed for offscreen content if (offscreenContent) { using (Bitmap tmpBitmap = Image.FromHbitmap(safeDibSectionHandle.DangerousGetHandle())) { // Create a new bitmap which has a transparent background returnBitmap = ImageHelper.CreateEmpty(tmpBitmap.Width, tmpBitmap.Height, PixelFormat.Format32bppArgb, Color.Transparent, tmpBitmap.HorizontalResolution, tmpBitmap.VerticalResolution); // Content will be copied here using (Graphics graphics = Graphics.FromImage(returnBitmap)) { // For all screens copy the content to the new bitmap foreach (Screen screen in Screen.AllScreens) { Rectangle screenBounds = screen.Bounds; // Make sure the bounds are offsetted to the capture bounds screenBounds.Offset(-captureBounds.X, -captureBounds.Y); graphics.DrawImage(tmpBitmap, screenBounds, screenBounds.X, screenBounds.Y, screenBounds.Width, screenBounds.Height, GraphicsUnit.Pixel); } } } } else { // All screens, which are inside the capture, are of equal size // assign image to Capture, the image will be disposed there.. returnBitmap = Image.FromHbitmap(safeDibSectionHandle.DangerousGetHandle()); } // We got through the capture without exception success = true; break; } catch (ExternalException ee) { LOG.Warn("Problem getting bitmap at try " + i + " : ", ee); exception = ee; } } if (!success) { LOG.Error("Still couldn't create Bitmap!"); if (exception != null) { throw exception; } } } } } return(returnBitmap); }
/// <summary> /// This method will use User32 code to capture the specified captureBounds from the screen /// </summary> /// <param name="captureBounds">Rectangle with the bounds to capture</param> /// <returns>Bitmap which is captured from the screen at the location specified by the captureBounds</returns> public static Bitmap CaptureRectangle(Rectangle captureBounds) { Bitmap returnBitmap = null; if (captureBounds.Height <= 0 || captureBounds.Width <= 0) { LOG.Warn("Nothing to capture, ignoring!"); return(null); } else { LOG.Debug("CaptureRectangle Called!"); } // .NET GDI+ Solution, according to some post this has a GDI+ leak... // See http://connect.microsoft.com/VisualStudio/feedback/details/344752/gdi-object-leak-when-calling-graphics-copyfromscreen // Bitmap capturedBitmap = new Bitmap(captureBounds.Width, captureBounds.Height); // using (Graphics graphics = Graphics.FromImage(capturedBitmap)) { // graphics.CopyFromScreen(captureBounds.Location, Point.Empty, captureBounds.Size, CopyPixelOperation.CaptureBlt); // } // capture.Image = capturedBitmap; // capture.Location = captureBounds.Location; // "P/Invoke" Solution for capturing the screen IntPtr hWndDesktop = User32.GetDesktopWindow(); // get te hDC of the target window IntPtr hDCDesktop = User32.GetWindowDC(hWndDesktop); // Make sure the last error is set to 0 Win32.SetLastError(0); // create a device context we can copy to IntPtr hDCDest = GDI32.CreateCompatibleDC(hDCDesktop); // Check if the device context is there, if not throw an error with as much info as possible! if (hDCDest == IntPtr.Zero) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateCompatibleDC", captureBounds); // Cleanup User32.ReleaseDC(hWndDesktop, hDCDesktop); // throw exception throw exceptionToThrow; } // Create BitmapInfoHeader for CreateDIBSection BitmapInfoHeader bmi = new BitmapInfoHeader(captureBounds.Width, captureBounds.Height, 24); // Make sure the last error is set to 0 Win32.SetLastError(0); // create a bitmap we can copy it to, using GetDeviceCaps to get the width/height IntPtr bits0; // not used for our purposes. It returns a pointer to the raw bits that make up the bitmap. IntPtr hDIBSection = GDI32.CreateDIBSection(hDCDesktop, ref bmi, BitmapInfoHeader.DIB_RGB_COLORS, out bits0, IntPtr.Zero, 0); if (hDIBSection == IntPtr.Zero) { // Get Exception before the error is lost Exception exceptionToThrow = CreateCaptureException("CreateDIBSection", captureBounds); exceptionToThrow.Data.Add("hdcDest", hDCDest.ToInt32()); exceptionToThrow.Data.Add("hdcSrc", hDCDesktop.ToInt32()); // clean up GDI32.DeleteDC(hDCDest); User32.ReleaseDC(hWndDesktop, hDCDesktop); // Throw so people can report the problem throw exceptionToThrow; } else { // select the bitmap object and store the old handle IntPtr hOldObject = GDI32.SelectObject(hDCDest, hDIBSection); // bitblt over (make copy) GDI32.BitBlt(hDCDest, 0, 0, captureBounds.Width, captureBounds.Height, hDCDesktop, captureBounds.X, captureBounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); // restore selection (old handle) GDI32.SelectObject(hDCDest, hOldObject); // clean up GDI32.DeleteDC(hDCDest); User32.ReleaseDC(hWndDesktop, hDCDesktop); // get a .NET image object for it // A suggestion for the "A generic error occurred in GDI+." E_FAIL/0×80004005 error is to re-try... bool success = false; ExternalException exception = null; for (int i = 0; i < 3; i++) { try { // assign image to Capture, the image will be disposed there.. returnBitmap = Bitmap.FromHbitmap(hDIBSection); success = true; break; } catch (ExternalException ee) { LOG.Warn("Problem getting bitmap at try " + i + " : ", ee); exception = ee; } } if (!success) { LOG.Error("Still couldn't create Bitmap!"); throw exception; } // free up the Bitmap object GDI32.DeleteObject(hDIBSection); } return(returnBitmap); }
/// <summary> /// Redraws the bitmap overlay. /// </summary> /// <returns>True if successful, false otherwise.</returns> protected bool RefreshBitmap() { // The bitmap must be 32-bit RGBA if (_bitmap == null || _bitmap.PixelFormat != PixelFormat.Format32bppArgb) { return(false); } // Create a memory DC that's compatible with the screen IntPtr screenDC = Win32.GetDC(IntPtr.Zero); if (screenDC == IntPtr.Zero) { return(false); } IntPtr memDC = GDI32.CreateCompatibleDC(screenDC); if (memDC == IntPtr.Zero) { Win32.ReleaseDC(IntPtr.Zero, screenDC); return(false); } // Prepare to draw the bitmap IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; bool success = false; try { // Select the bitmap into the memory DC hBitmap = _bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap oldBitmap = GDI32.SelectObject(memDC, hBitmap); // Call UpdateLayeredWindow to effectively blit the contents of the memory DC into the form while performing alpha blending Win32.POINT windowTopLeft = new Win32.POINT(Left, Top); Win32.SIZE bitmapSize = new Win32.SIZE(_bitmap.Width, _bitmap.Height); Win32.POINT bitmapTopLeft = new Win32.POINT(0, 0); byte blendAlpha = 0; if (_bitmapOpacity < 0) { blendAlpha = 0; } else if (_bitmapOpacity > 1) { blendAlpha = 255; } else { blendAlpha = (byte)(_bitmapOpacity * 255); } GDI32.BLENDFUNCTION blendFunc = new GDI32.BLENDFUNCTION(); blendFunc.BlendOp = GDI32.AC_SRC_OVER; blendFunc.BlendFlags = 0; blendFunc.SourceConstantAlpha = blendAlpha; blendFunc.AlphaFormat = GDI32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDC, ref windowTopLeft, ref bitmapSize, memDC, ref bitmapTopLeft, 0, ref blendFunc, Win32.ULW_ALPHA); success = true; } finally { // Clean up the resources if (hBitmap != IntPtr.Zero) { GDI32.SelectObject(memDC, oldBitmap); GDI32.DeleteObject(hBitmap); } GDI32.DeleteDC(memDC); Win32.ReleaseDC(IntPtr.Zero, screenDC); } return(success); }
public GDI32HBitmap(User32DeviceContext hdcSrc, Size size) { this.hdcSrc = hdcSrc; this.size = size; this.hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc.DC, size.Width, size.Height); }
public GDI32DeviceContext(User32DeviceContext hdcSrc) { this.hdcSrc = hdcSrc; this.dc = GDI32.CreateCompatibleDC(hdcSrc.DC); }
public static extern BOOL UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref GDI32.BLENDFUNCTION pblend, Int32 dwFlags);