} // end method Desktop /// <summary> /// Captures the desktop work area to a bitmap image /// </summary> /// <returns>bitmap image of desktop work area</returns> public static Bitmap DesktopWA(Control ctl) { Rectangle wa = Screen.GetWorkingArea(ctl); // working area of screen IntPtr desktopHWND = USER32.GetDesktopWindow(); // window handle for desktop return(Window(desktopHWND, wa.X, wa.Y, wa.Width, wa.Height)); // return bitmap for desktop }
/// <summary> /// Captures the desktop to a bitmap image /// </summary> /// <returns>bitmap image of the desktop</returns> public static Bitmap Desktop() { int width = USER32.GetSystemMetrics(USER32.SM_CXSCREEN); // width of desktop int height = USER32.GetSystemMetrics(USER32.SM_CYSCREEN); // height of desktop IntPtr desktopHWND = USER32.GetDesktopWindow(); // window handle for desktop return(Window(desktopHWND, 0, 0, width, height)); // return bitmap for desktop } // end method Desktop
public static Bitmap GetDesktopImage() { return(GetImageFromHandle( //Here we get the handle to the desktop device context. USER32.GetDesktopWindow(), //We pass SM_CXSCREEN constant to GetSystemMetrics to get the X coordinates of screen. USER32.GetSystemMetrics(SCREENCAPTURE.SM_CXSCREEN), //We pass SM_CYSCREEN constant to GetSystemMetrics to get the Y coordinates of screen. USER32.GetSystemMetrics(SCREENCAPTURE.SM_CYSCREEN) )); }
public static Bitmap Control(System.Windows.Forms.Control ctl, bool client, bool under) { Bitmap bmp; // capture bitmap Rectangle ctlR; // capture area rectangle in control coordinates Rectangle scrR; // capture area rectangle in screen coordinates // get capture rectangle in control // coordinates and in screen coordinates if (client) // if capturing client area { ctlR = ctl.ClientRectangle; // get rectangle in control coordinates scrR = ctl.RectangleToScreen(ctlR); // get rectangle in screen coordinates } else // if capturing entire control { scrR = ctl.Bounds; // get rectangle in parent coordinates if (ctl.Parent != null) // if parent exists { scrR = ctl.Parent.RectangleToScreen(scrR); // map to screen coordinates } ctlR = ctl.RectangleToClient(scrR); // get rectangle in control coordinates } // capture an area under the control if (under) // if capture area is under control { bool prvV = ctl.Visible; // save control visibility if (prvV) // if control visible { ctl.Visible = false; // make control invisible Thread.Sleep(m_HDelay); // allow time for control to become invisible // prior to image capture } // Capture the bitmap using desktop window handle and screen coordinates // for the capture area. Note, the control window handle can NOT be used // for capturing an area under the control. IntPtr desktopHWND = USER32.GetDesktopWindow(); // get window handle for desktop bmp = Window(desktopHWND, scrR); // get bitmap for capture area under control if (ctl.Visible != prvV) // if control visibility was changed { ctl.Visible = prvV; // restore previous visibility } } // capture an area on the control else // if capture area not under control { // Capture the bitmap using control window handle and control coordinates // for capture area. bmp = Window(ctl.Handle, ctlR); // get bitmap using control window handle } return(bmp); // return requested bitmap }
public static Bitmap GetImageFromHandle(IntPtr ptr, int width, int height) { //Variable to keep the handle to bitmap. IntPtr hBitmap; //Here we get the handle to the control device context. IntPtr hDC = USER32.GetDC(ptr); //Here we make a compatible device context in memory for screen device context. IntPtr hMemDC = GDI32.CreateCompatibleDC(hDC); //We create a compatible bitmap of screen size using screen device context. hBitmap = GDI32.CreateCompatibleBitmap(hDC, width, height); //As hBitmap is IntPtr we can not check it against null. For this purspose IntPtr.Zero is used. if (hBitmap != IntPtr.Zero) { //Here we select the compatible bitmap in memeory device context and keeps the refrence to Old bitmap. IntPtr hOld = (IntPtr)GDI32.SelectObject(hMemDC, hBitmap); //We copy the Bitmap to the memory device context. GDI32.BitBlt(hMemDC, 0, 0, width, height, hDC, 0, 0, IMAGECAPTURE.SRCCOPY); //We select the old bitmap back to the memory device context. GDI32.SelectObject(hMemDC, hOld); //We delete the memory device context. GDI32.DeleteDC(hMemDC); //We release the screen device context. USER32.ReleaseDC(USER32.GetDesktopWindow(), hDC); //Image is created by Image bitmap handle and stored in local variable. Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap); //Release the memory for compatible bitmap. GDI32.DeleteObject(hBitmap); //This statement runs the garbage collector manually. GC.Collect(); //Return the bitmap return(bmp); } //If hBitmap is null retunrn null. return(null); }
/// <summary> /// Captures the specified area of the control or whats underneath /// </summary> /// <param name="ctl">Control to capture</param> /// <param name="client">If true capture only client area else entire control.</param> /// <param name="under">If true capture specified area underneath the control else whats on the control.</param> /// <returns>bitmap image of the control or whats underneath the control</returns> public static void Control(System.Windows.Forms.Form ctl) { Bitmap bmp; // capture bitmap Rectangle ctlR; // capture area rectangle in control coordinates Rectangle scrR; // capture area rectangle in screen coordinates int m_hDelay = 0; scrR = ctl.Bounds; // get rectangle in parent coordinates if (ctl.Parent != null) // if parent exists { scrR = ctl.Parent.RectangleToScreen(scrR); // map to screen coordinates } ctlR = ctl.RectangleToClient(scrR); // get rectangle in control coordinates // capture an area under the control ctl.Opacity = 0; System.Threading.Thread.Sleep(m_hDelay); Single oprvV = (float)ctl.Opacity; // save control visibility if (oprvV > 0) // if control visible { ctl.Opacity = 0; ctl.Refresh(); } IntPtr desktopHWND = USER32.GetDesktopWindow(); // get window handle for desktop bmp = Window(desktopHWND, scrR); // get bitmap for capture area under control if (ctl.Opacity < 1) { ctl.BackgroundImage = bmp; ctl.Refresh(); System.Threading.Thread.Sleep(m_hDelay); ctl.Opacity = 1; } System.GC.Collect(); }
static Icon CaptureCursor() { USER32.CURSORINFO cursorInfo = new USER32.CURSORINFO(); cursorInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(cursorInfo); if (!USER32.GetCursorInfo(out cursorInfo)) { return(null); } if (cursorInfo.flags != USER32.CURSORINFOFlags.CURSOR_SHOWING) { return(null); } IntPtr hicon = USER32.CopyIcon(cursorInfo.hCursor); if (hicon == IntPtr.Zero) { return(null); } USER32.ICONINFO iconInfo; if (!USER32.GetIconInfo(hicon, out iconInfo)) { return(null); } //x = cursorInfo.ptScreenPos.X - ((int)iconInfo.xHotspot); //y = cursorInfo.ptScreenPos.Y - ((int)iconInfo.yHotspot); using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask)) { // Is this a monochrome cursor? if (maskBitmap.Height == maskBitmap.Width * 2) { Bitmap resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width); Graphics desktopGraphics = Graphics.FromHwnd(USER32.GetDesktopWindow()); IntPtr desktopHdc = desktopGraphics.GetHdc(); IntPtr maskHdc = GDI32.CreateCompatibleDC(desktopHdc); IntPtr oldPtr = GDI32.SelectObject(maskHdc, maskBitmap.GetHbitmap()); using (Graphics resultGraphics = Graphics.FromImage(resultBitmap)) { IntPtr resultHdc = resultGraphics.GetHdc(); // These two operation will result in a black cursor over a white background. // Later in the code, a call to MakeTransparent() will get rid of the white background. GDI32.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, TernaryRasterOperations.SRCCOPY); GDI32.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, TernaryRasterOperations.SRCINVERT); resultGraphics.ReleaseHdc(resultHdc); } IntPtr newPtr = GDI32.SelectObject(maskHdc, oldPtr); USER32.DeleteObject(newPtr); GDI32.DeleteDC(maskHdc); desktopGraphics.ReleaseHdc(desktopHdc); // Remove the white background from the BitBlt calls, // resulting in a black cursor over a transparent background. resultBitmap.MakeTransparent(Color.White); return(Icon.FromHandle(resultBitmap.GetHicon())); } } return(Icon.FromHandle(hicon)); }