Winforms -包含框架的窗口图像(BitBlt)



我在网上找到了一些代码,可以抓取当前窗口并将其复制到位图中。我在下面附上了相关的部分。目前它复制的客户端区域,但我想得到框架以及。有办法解决这个问题吗?所以我想快照整个窗口,包括最大化按钮,控制按钮等。

// Capture snapshot of the form...
if (base.IsHandleCreated)
{
    //
    // Get DC of the form...
    IntPtr srcDc = GetDC(Handle);
    //
    // Create bitmap to store image of form...
    var bmp = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
    //
    // Create a GDI+ context from the created bitmap...
    using (Graphics g = Graphics.FromImage(bmp))
    {
        //
        // Copy image of form into bitmap...
        IntPtr bmpDc = g.GetHdc();
        BitBlt(bmpDc, 0, 0, bmp.Width, bmp.Height, srcDc, 0, 0, 0x00CC0020 /* SRCCOPY */);

只需使用窗体的DrawToBitmap()方法:

        using (var bmp = new Bitmap(this.Width, this.Height)) {
            this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
            bmp.Save("c:/temp/test.png");
        }

Graphics.CopyFromScreen()是另一种方法,类似于您现在所做的。它实际上是从屏幕上复制图像,而不是要求表单将自己绘制成位图。同样的缺点是,表单需要是可见的。

最新更新