使用黑名单捕获当前应用程序屏幕截图

时间:2018-07-26 13:57:31

标签: c# winforms

我可以使用链接中提到的以下任何过程来捕获屏幕截图

Capture screenshot of active window?

但是我想通过消除表单中的某些控件来捕获屏幕截图。

示例:如果我不希望表单中存在文本框,则在屏幕截图中应将其标记为黑色。

2 个答案:

答案 0 :(得分:1)

如果控件的父级是表单,则可以执行以下操作:

Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
        Rectangle formScreenRect = RectangleToScreen(this.ClientRectangle);
        int offsetX = formScreenRect.Left - this.Left;
        int offsetY = formScreenRect.Top - this.Top;
        Rectangle textBoxRect = new Rectangle(textBox1.Left + offsetX, 
                                              textBox1.Top + offsetY,
                                              textBox1.Width, textBox1.Height);
        g.FillRectangle(Brushes.Black, textBoxRect);
    }
    // Save the image or do whatever you want with it.
    bitmap.Save(@"C:\test.png", ImageFormat.Png);
}

这将计算TextBox相对于表单大小(而不是客户端大小)的位置,然后使用FillRectangle()用您选择的颜色的矩形覆盖它。

输出:

Form

答案 1 :(得分:0)

@Ahmed Abdelhameed已经回答了您的问题,但是如果您不希望出现黑框使它看起来更好,则可以执行以下操作 使用答案 The post you tagged

您可以简单地执行以下操作:

textbox1.visible = false;

// Code to take the snapshot and save it

textbox1.visible = true;