没有活动窗口的屏幕截图

我试图写一个使用WinForm或WPF窗口的放大镜应用程序。 这个想法是拖动窗口在屏幕上的一个点,并放大它。 我知道它存在商业,但需要build立一个定制的版本。 我遇到的挑战是捕获活动应用程序后面的屏幕图像。 我已经find代码来捕获下面的屏幕图像。 但它包括活动窗口

{ Rectangle bounds = new Rectangle(this.Location, this.Size); Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height); Graphics g = Graphics.FromImage(bitmap); g.CopyFromScreen(this.Location, Point.Empty, bounds.Size); Bitmap bp2 = new Bitmap(bitmap); // local copy of image... pictureBox1.Image = bp2; } 

添加语句来隐藏活动的应用程序可以更正图像捕捉,但会引入一个我想避免的画面闪烁。 (修改后的代码)

  { this.StartPosition = FormStartPosition.Manual; // get current window location Point cur = this.Location; this.Location = new Point(-500, -500); // hide the active app off screen. Rectangle bounds = new Rectangle(cur, this.Size); Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height); Graphics g = Graphics.FromImage(bitmap); g.CopyFromScreen(cur, Point.Empty, bounds.Size); Bitmap bp2 = new Bitmap(bitmap); // local copy of image... pictureBox1.Image = bp2; this.Location = cur; // restore application location } 

有人可以build议一个替代捕获屏幕区域,在一个活动的窗口后面?

谢谢。

Solutions Collecting From Web of "没有活动窗口的屏幕截图"

包装Magnification API非常有用。 我创建了一个Winforms控件。 为您的项目添加一个新类,并粘贴下面显示的代码。 编译。 将新的控件从工具箱的顶部拖放到表单上。

TrackMouse属性控制放大显示屏幕的哪一部分。 设置为False时,它会放大控件所覆盖的区域,使其像放大镜一样。 设置为True,按照鼠标操作,就像Windows的放大镜一样。

“放大”属性控制放大倍数。 您可以使用鼠标滚轮进行调整。

您放置它的窗体应该将其TopMost属性设置为True。 您可能需要修改其“地区”属性以使其类似于望远镜。

 using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; class Magnifier : Control { public Magnifier() { if (!MagInitialize()) throw new NotSupportedException(); timer = new Timer { Interval = 45 }; timer.Tick += (o, ea) => { if (trackMouse) setSource(false); else Invalidate(); }; } [DefaultValue(false)] public bool TrackMouse { get { return trackMouse; } set { trackMouse = value; setSource(false); } } [DefaultValue(2.0f)] public float Magnification { get { return magnification; } set { magnification = Math.Max(1, value); setSource(true); } } protected override CreateParams CreateParams { get { var cp = base.CreateParams; if (!this.DesignMode) { cp.ClassName = "Magnifier"; //cp.Style |= MS_SHOWMAGNIFIEDCURSOR; this.SetStyle(ControlStyles.UserPaint, true); } return cp; } } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); if (!this.DesignMode) { setSource(true); this.FindForm().LocationChanged += ParentLocationChanged; timer.Start(); } } protected override void Dispose(bool disposing) { if (disposing) { var frm = this.FindForm(); if (frm != null) frm.LocationChanged -= ParentLocationChanged; timer.Dispose(); MagUninitialize(); } base.Dispose(disposing); } private void ParentLocationChanged(object sender, EventArgs e) { if (!trackMouse) setSource(false); } protected override void OnSizeChanged(EventArgs e) { setSource(false); base.OnSizeChanged(e); } protected override void OnMouseWheel(MouseEventArgs e) { this.Magnification += e.Delta / 100f; ((HandledMouseEventArgs)e).Handled = true; } private void setSource(bool newmag) { if (!this.IsHandleCreated || this.DesignMode) return; if (newmag) { var xform = new MAGTRANSFORM(); xform.v11 = xform.v22 = magnification; xform.v33 = 1.0f; MagSetWindowTransform(this.Handle, ref xform); } Point center; if (trackMouse) center = Cursor.Position; else { var rc = this.RectangleToScreen(this.Bounds); center = new Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2); } var scr = Screen.FromPoint(center); var rect = new RECT(); rect.left = Math.Max(scr.Bounds.Left, center.X - (int)(this.Width / magnification / 2)); rect.top = Math.Max(scr.Bounds.Top, center.Y - (int)(this.Height / magnification / 2)); rect.right = rect.left + (int)(this.Width / magnification); if (rect.right > scr.Bounds.Right) { rect.right = scr.Bounds.Right; rect.left = rect.right - (int)(this.Width / magnification); } rect.bottom = center.Y + (int)(this.Height / magnification); if (rect.bottom > scr.Bounds.Bottom) { rect.bottom = scr.Bounds.Bottom; rect.top = rect.bottom - (int)(this.Height / magnification); } MagSetWindowSource(this.Handle, ref rect); this.Invalidate(); } private Timer timer; private bool trackMouse; private float magnification = 2.0f; private struct RECT { public int left, top, right, bottom; } private struct MAGTRANSFORM { public float v11, v12, v13; public float v21, v22, v23; public float v31, v32, v33; } [DllImport("magnification.dll")] private static extern bool MagInitialize(); [DllImport("magnification.dll")] private static extern bool MagUninitialize(); [DllImport("magnification.dll")] private static extern bool MagSetWindowSource(IntPtr hWnd, ref RECT rc); [DllImport("magnification.dll")] private static extern bool MagSetWindowTransform(IntPtr hWnd, ref MAGTRANSFORM xform); private const int MS_SHOWMAGNIFIEDCURSOR = 1; private const int MS_CLIPAROUNDCURSOR = 2; private const int MS_INVERTCOLORS = 4; }