C# 如何捕捉包括标题栏在内的窗口快照
下面是我的例行程序,工作正常,但例行程序不生成包括标题栏的窗口图像。所以请指导我需要在代码中更改什么C# 如何捕捉包括标题栏在内的窗口快照,c#,screenshot,C#,Screenshot,下面是我的例行程序,工作正常,但例行程序不生成包括标题栏的窗口图像。所以请指导我需要在代码中更改什么 protected override void WndProc(ref Message m) { if (m.Msg == WM_COMMAND && m.WParam.ToInt32() == SC_MINIMIZE) { OnMinimize(EventArgs.Empty);
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COMMAND && m.WParam.ToInt32() == SC_MINIMIZE)
{
OnMinimize(EventArgs.Empty);
}
base.WndProc(ref m);
}
protected virtual void OnMinimize(EventArgs e)
{
Rectangle r = this.RectangleToScreen(ClientRectangle);
if (_lastSnapshot == null)
{
_lastSnapshot = new Bitmap(r.Width, r.Height);
}
using (Image windowImage = new Bitmap(r.Width, r.Height))
using (Graphics windowGraphics = Graphics.FromImage(windowImage))
using (Graphics tipGraphics = Graphics.FromImage(_lastSnapshot))
{
windowGraphics.CopyFromScreen(new Point(r.Left, r.Top), new Point(0, 0), new Size(r.Width, r.Height));
windowGraphics.Flush();
tipGraphics.DrawImage(windowImage, 0, 0, r.Width, r.Height);
}
}
更新
您的代码可以工作,但左上角的部分不正常。所以在这里,我上传的图像,我已经用你的代码生成。请看一下,告诉我需要在代码中修复什么。表单宽度不正确。谢谢 更新
只需获取标题栏的高度并将其添加到要捕获区域的高度(并从图像的当前顶部减去它-检查下面的源以了解更改):
您的代码可以工作,但左上角的部分不正常。所以在这里,我上传的图像,我已经用你的代码生成。请看一下,告诉我需要在代码中修复什么。表单宽度不正确。thanksIt看起来您仍然设置了
\u lastsnashot=新位图(100100)代码>?或者要保存的图形对象的宽度,即\u lastSnapshot
。验证是否在所有位置都以与我相同的方式设置其宽度。在应用显示标题栏的更改之前,宽度是否正确显示?这次我确实尝试了上面的代码,它对我来说确实很好。它在这里捕获了整个表单^^^事实上,我只是将上面的代码复制并粘贴到我的应用程序中,得到了一个格式正确的图像。我只是复制并粘贴了你的代码…没有任何变化,但从你的代码生成的图片看起来不正常。你正在初始化方法之外的\u lastsnashot
。因此,当它进入方法时,它不是空的,并且它的宽度永远不会被正确设置。删除对\u lastSnapshot==null
的检查。否则应用程序将永远不会重置新位图的大小。请看我的编辑。问题仍然存在,我可以理解为什么没有全宽。在左侧和右侧有不可见的非客户区,这就是为什么右侧图片看起来很奇怪。你知道如何使用表单的非客户区宽度计算宽度吗?
Bitmap bmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
bmp.Save(@"d:\Zapps.bmp", ImageFormat.Bmp);
protected virtual void OnMinimize(EventArgs e)
{
Rectangle r = this.RectangleToScreen(this.ClientRectangle);
int titleHeight = r.Top - this.Top;
_lastSnapshot = new Bitmap(r.Width, r.Height);
using (Image windowImage = new Bitmap(r.Width, r.Height + titleHeight))
using (Graphics windowGraphics = Graphics.FromImage(windowImage))
using (Graphics tipGraphics = Graphics.FromImage(_lastSnapshot))
{
windowGraphics.CopyFromScreen(new Point(r.Left, r.Top - titleHeight),
new Point(0, 0), new Size(r.Width, r.Height + titleHeight));
windowGraphics.Flush();
tipGraphics.DrawImage(windowImage, 0, 0, r.Width, r.Height + titleHeight);
_lastSnapshot.Save(@".\tmp.bmp", ImageFormat.Bmp);
}
}