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 void DrawMonitor(KeyValuePair <Pair <VirtualDesktop, HMONITOR>, ObservableCollection <DesktopWindow> > desktopMonitor, out float ScreenScalingFactorVert, out int mX, out int mY, out IEnumerable <Rectangle> gridGenerator) { HMONITOR m = desktopMonitor.Key.Item2; int windowsCount = desktopMonitor.Value.Count(); User32.MONITORINFOEX info = new User32.MONITORINFOEX(); info.cbSize = (uint)Marshal.SizeOf(info); User32.GetMonitorInfo(m, ref info); Gdi32.SafeHDC hdc = Gdi32.CreateDC(info.szDevice); int LogicalScreenHeight = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.VERTRES); int PhysicalScreenHeight = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.DESKTOPVERTRES); int LogicalScreenWidth = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.HORZRES); int PhysicalScreenWidth = Gdi32.GetDeviceCaps(hdc, Gdi32.DeviceCap.DESKTOPHORZRES); hdc.Close(); float ScreenScalingFactorHoriz = (float)PhysicalScreenWidth / (float)LogicalScreenWidth; ScreenScalingFactorVert = (float)PhysicalScreenHeight / (float)LogicalScreenHeight; mX = info.rcWork.X + MarginLeft; mY = info.rcWork.Y + MarginTop; int mWidth = info.rcWork.Width - MarginLeft - MarginRight; int mHeight = info.rcWork.Height - MarginTop - MarginBottom; Layout mCurrentLayout; try { mCurrentLayout = Layouts[desktopMonitor.Key]; } catch { Layouts.Add(desktopMonitor.Key, Layout.Tall); mCurrentLayout = Layouts[desktopMonitor.Key]; } if (!Factors.ContainsKey(desktopMonitor.Key)) { Factors[desktopMonitor.Key] = 0; } gridGenerator = GridGenerator(mWidth, mHeight, windowsCount, Factors[desktopMonitor.Key], mCurrentLayout, LayoutPadding); }
/// <summary> /// /// </summary> /// <param name="screenNb"></param> /// <param name="quality"></param> /// <returns></returns> public static ScreenCaptureMessage CaptureScreen(int screenNb, int quality) { var bounds = Screen.AllScreens[screenNb].Bounds; var screen = new Bitmap( bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb); using (var dest = Graphics.FromImage(screen)) { var destPtr = dest.GetHdc(); var srcPtr = Gdi32.CreateDC("DISPLAY", null, null, IntPtr.Zero); Gdi32.BitBlt(destPtr, 0, 0, bounds.Width, bounds.Height, srcPtr, bounds.X, bounds.Y, Gdi32.ROP_COPY); Gdi32.DeleteDC(srcPtr); dest.ReleaseHdc(destPtr); } var compressed = ImageHelpers.CompressImage(screen, quality); screen.Dispose(); return(new ScreenCaptureMessage { ScreenNumber = screenNb, Quality = quality, Width = bounds.Width, Height = bounds.Height, RawImage = compressed }); }
internal unsafe Screen(IntPtr monitor, Gdi32.HDC hdc) { Gdi32.HDC screenDC = hdc; if (!multiMonitorSupport || monitor == (IntPtr)PRIMARY_MONITOR) { // Single monitor system // bounds = SystemInformation.VirtualScreen; primary = true; deviceName = "DISPLAY"; } else { // Multiple monitor system var info = new User32.MONITORINFOEXW { cbSize = (uint)sizeof(User32.MONITORINFOEXW) }; User32.GetMonitorInfoW(monitor, ref info); bounds = info.rcMonitor; primary = ((info.dwFlags & User32.MONITORINFOF.PRIMARY) != 0); deviceName = new string(info.szDevice); if (hdc.IsNull) { screenDC = Gdi32.CreateDC(deviceName, null, null, IntPtr.Zero); } } hmonitor = monitor; bitDepth = Gdi32.GetDeviceCaps(screenDC, Gdi32.DeviceCapability.BITSPIXEL); bitDepth *= Gdi32.GetDeviceCaps(screenDC, Gdi32.DeviceCapability.PLANES); if (hdc != screenDC) { Gdi32.DeleteDC(screenDC); } }
internal Screen(IntPtr monitor, IntPtr hdc) { IntPtr screenDC = hdc; if (!multiMonitorSupport || monitor == (IntPtr)PRIMARY_MONITOR) { // Single monitor system // bounds = SystemInformation.VirtualScreen; primary = true; deviceName = "DISPLAY"; } else { // Multiple monitor system var info = new NativeMethods.MONITORINFOEX(); SafeNativeMethods.GetMonitorInfo(new HandleRef(null, monitor), info); bounds = Rectangle.FromLTRB(info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.right, info.rcMonitor.bottom); primary = ((info.dwFlags & MONITORINFOF_PRIMARY) != 0); deviceName = new string(info.szDevice); deviceName = deviceName.TrimEnd((char)0); if (hdc == IntPtr.Zero) { screenDC = Gdi32.CreateDC(deviceName, null, null, IntPtr.Zero); } } hmonitor = monitor; bitDepth = Gdi32.GetDeviceCaps(screenDC, Gdi32.DeviceCapability.BITSPIXEL); bitDepth *= Gdi32.GetDeviceCaps(screenDC, Gdi32.DeviceCapability.PLANES); if (hdc != screenDC) { Gdi32.DeleteDC(screenDC); } }