public static void CaptureScreen(MonitorInfo mi, string fileName) { mi.WithMonitorHdc((m, hdc) => { var screenBmp = new Bitmap(m.Bounds.Width, m.Bounds.Height, PixelFormat.Format32bppArgb); using (var destGraphics = Graphics.FromImage(screenBmp)) { var monitorDC = new HandleRef(null, hdc); var destDC = new HandleRef(null, destGraphics.GetHdc()); var result = BitBlt(destDC, 0, 0, m.Bounds.Width, m.Bounds.Height, monitorDC, 0, 0, unchecked ((int)BITBLT_SRCCOPY)); if (result == 0) { throw new Win32Exception(); } } screenBmp.Save(fileName); }); }
public static Bitmap CaptureScreen(MonitorInfo mi) { Bitmap screenBmp = default(Bitmap); mi.WithMonitorHdc((m, hdc) => { screenBmp = new Bitmap(m.Bounds.Width, m.Bounds.Height, PixelFormat.Format32bppArgb); using (var destGraphics = Graphics.FromImage(screenBmp)) { var monitorDC = new HandleRef(null, hdc); var destDC = new HandleRef(null, destGraphics.GetHdc()); var result = BitBlt(destDC, 0, 0, m.Bounds.Width, m.Bounds.Height, monitorDC, 0, 0, unchecked ((int)BITBLT_SRCCOPY)); if (result == 0) { throw new Win32Exception(); } } }); return(screenBmp); }