简体   繁体   English

如何捕获包含标题栏的窗口快照

[英]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. 所以在这里我要上传使用ur代码生成的图像。 please have a look and tell me what i need to fix in the code. 请看看并告诉我我需要在代码中修复的问题。 form width is not coming properly. 表格宽度不正确。 thanks 谢谢 在此处输入图片说明

UPDATE 更新

    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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM