如何捕获窗口的快照,包括标题栏 [英] How to capture snap of window including title bar
问题描述
下面是我的例程,它工作正常,但例程没有生成包括标题栏的窗口图像.所以请指导我在代码中需要更改什么.
below is my routine which works fine but the routine is not generating the image of window including title bar. so guide me what i need to change in code.
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);
}
}
更新
您的代码有效,但左上角的某些部分不正常.所以在这里我上传我使用你的代码生成的图像.请看看并告诉我我需要在代码中修复什么.表单宽度不正确.谢谢
UPDATE
ur code works but some top left portion is not ok. so here i am uploading the image which i have generated using ur code. please have a look and tell me what i need to fix in the code. form width is not coming properly. thanks
Bitmap bmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
bmp.Save(@"d:\Zapps.bmp", ImageFormat.Bmp);
推荐答案
简单的获取标题栏的高度,加上要截取的区域的高度(并从当前图片的顶部减去——勾选以下更改的来源):
Simply get the height of the title bar and add it to the height of the area to be captured (and subtract it from the current top of the image - check the source below for the changes):
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);
}
}
这篇关于如何捕获窗口的快照,包括标题栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!