public static Image ScreenCapture(IntPtr Window) { PixelFormat GrabFormat; Bitmap windowBitmap; switch (m_ScreenPixelFormat) { case PixelFormat.Format16bppRgb555: case PixelFormat.Format16bppRgb565: case PixelFormat.Format24bppRgb: case PixelFormat.Format32bppArgb: case PixelFormat.Format32bppPArgb: case PixelFormat.Format32bppRgb: GrabFormat = m_ScreenPixelFormat; break; case PixelFormat.Format8bppIndexed: GrabFormat = PixelFormat.Format32bppArgb; break; default: throw new Exception("Format " + m_ScreenPixelFormat.ToString() + " is not supported"); } int Width; int Height; int X; int Y; if (Window == IntPtr.Zero) { Width = Screen.PrimaryScreen.Bounds.Width; Height = Screen.PrimaryScreen.Bounds.Height; X = 0; Y = 0; } else { NM.tagRect WindowRect; if (NativeVersion.IsWindowsVistaOrHigher && NM.IsTopLevelWindow(Window)) { bool DWMEnabled; NM.DwmIsCompositionEnabled(out DWMEnabled); if (DWMEnabled) { NM.DwmGetWindowAttribute(Window, NM.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out WindowRect, Marshal.SizeOf(typeof(NM.tagRect))); } else { NM.GetWindowRect(Window, out WindowRect); } } else { NM.GetWindowRect(Window, out WindowRect); } Width = WindowRect.right - WindowRect.left; Height = WindowRect.bottom - WindowRect.top; X = WindowRect.left; Y = WindowRect.top; } windowBitmap = new Bitmap(Width, Height, GrabFormat); GetWindowBitmap(Window, X, Y, ref windowBitmap, true, false); if (m_ScreenPixelFormat == PixelFormat.Format8bppIndexed) { WuQuantizer quantizer = new WuQuantizer(); Image quantized = quantizer.QuantizeImage(windowBitmap); return(quantized); } else { return(windowBitmap); } }
private static void CaptureVideo() { //TO DO set the defaults in the regristry for codec and install the SCPR codec //SCLS = MSU Screen Capture Lossless Codec v1.2 //SCPR = Infognition ScreenPressor if (m_IsCapturingVideo) { throw new Exception("Video capture already in progress"); } if (m_VideoFilename == "") { throw new Exception("Need to set the video file name property"); } int Width; int Height; int X; int Y; if (m_VideoCaptureWindow == IntPtr.Zero) { Width = Screen.PrimaryScreen.Bounds.Width; Height = Screen.PrimaryScreen.Bounds.Height; X = 0; Y = 0; } else { NM.tagRect WindowRect; if (NativeVersion.IsWindowsVistaOrHigher && NM.IsTopLevelWindow(m_VideoCaptureWindow)) { bool DWMEnabled; NM.DwmIsCompositionEnabled(out DWMEnabled); if (DWMEnabled) { NM.DwmGetWindowAttribute(m_VideoCaptureWindow, NM.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out WindowRect, Marshal.SizeOf(typeof(NM.tagRect))); } else { NM.GetWindowRect(m_VideoCaptureWindow, out WindowRect); } } else { NM.GetWindowRect(m_VideoCaptureWindow, out WindowRect); } Width = WindowRect.right - WindowRect.left; Height = WindowRect.bottom - WindowRect.top; X = WindowRect.left; Y = WindowRect.top; } VfW.Codec = m_VideoCodec; VfW.FrameRate = m_VideoFrameRate; VfW.Quality = m_VideoQuality; VfW.KeyFrameEvery = m_VideoKeyFrameEvery; int TimeToSleep; int Sleep = (int)(((float)1 / (float)m_VideoFrameRate) * (float)1000); Stopwatch timer = new Stopwatch(); m_IsCapturingVideo = true; Bitmap screen = VfW.Open(m_VideoFilename, Width, Height, m_VideoPixelFormat); try { while (m_IsCapturingVideo) { timer.Reset(); timer.Start(); GetWindowBitmap(m_VideoCaptureWindow, X, Y, ref screen, true, false); VfW.AddFrame(screen); timer.Stop(); TimeToSleep = Sleep - (int)timer.ElapsedMilliseconds - 4; if (TimeToSleep > 0) { Thread.Sleep(TimeToSleep); } } } finally { VfW.Close(); } }
public static NM.tagRect GetWindowRectangleDIP(IntPtr Window) { int Adjustment = 1; int TitleBarRight = 0; string WindowState = ""; NM.tagRect ControlRect; bool desktopWindowManagerEnabled; bool topLevelWindow = NM.IsTopLevelWindow(Window); if (topLevelWindow) { // Get the titlebar rectangle NM.TITLEBARINFO CurrentTitleBarInfo = new NM.TITLEBARINFO(); CurrentTitleBarInfo.cbSize = (uint)Marshal.SizeOf(CurrentTitleBarInfo); NM.GetTitleBarInfo(Window, ref CurrentTitleBarInfo); TitleBarRight = CurrentTitleBarInfo.rcTitleBar.right; // Get the windows current state NM.WindowPlacement CurrentWindowPlacement = new NM.WindowPlacement(); CurrentWindowPlacement.length = (uint)Marshal.SizeOf(CurrentWindowPlacement); NM.GetWindowPlacement(Window, ref CurrentWindowPlacement); if (CurrentWindowPlacement.showCmd.ToString() == "ShowMaximized") { WindowState = "Maximized"; } } //TODO screen capture broke for not toplevel windows / non dwm toplevel with dpi? NM.DwmIsCompositionEnabled(out desktopWindowManagerEnabled); if (desktopWindowManagerEnabled && topLevelWindow) { NM.DwmGetWindowAttribute(Window, NM.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out ControlRect, Marshal.SizeOf(typeof(NM.tagRect))); if (topLevelWindow && WindowState == "Maximized") { Adjustment += ControlRect.right - TitleBarRight; } ControlRect.left = ControlRect.left + Adjustment; ControlRect.top = ControlRect.top + Adjustment; ControlRect.right = ControlRect.right - Adjustment; ControlRect.bottom = ControlRect.bottom - Adjustment; } else { NM.GetWindowRect(Window, out ControlRect); if (topLevelWindow && WindowState == "Maximized") { Adjustment += ControlRect.right - TitleBarRight; } float ScreenScalingFactor; using (Graphics desktopGraphics = Graphics.FromHwnd(Window)) { IntPtr desktopDeviceContext = desktopGraphics.GetHdc(); int LogicalScreenHeight = NM.GetDeviceCaps(desktopDeviceContext, NM.DeviceCap.VERTRES); int PhysicalScreenHeight = NM.GetDeviceCaps(desktopDeviceContext, NM.DeviceCap.DESKTOPVERTRES); desktopGraphics.ReleaseHdc(); ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight; } ControlRect.left = (int)(Math.Round((float)(ControlRect.left + Adjustment) * ScreenScalingFactor)); ControlRect.top = (int)(Math.Round((float)(ControlRect.top + Adjustment) * ScreenScalingFactor)); ControlRect.right = (int)(Math.Round((float)(ControlRect.right - Adjustment) * ScreenScalingFactor)); ControlRect.bottom = (int)(Math.Round((float)(ControlRect.bottom - Adjustment) * ScreenScalingFactor)); } return(ControlRect); }