Form.Hide不会阻止表单包含在屏幕转储中

时间:2018-03-21 18:04:05

标签: c# winforms screenshot

问题

我使用以下代码执行屏幕转储。即使我用this.Hide隐藏表单本身,表单仍然包含在屏幕转储中,我不希望这样。

this.Hide(); //Hide to not include this form in the screen dump

try
{
    Rectangle bounds = Screen.GetBounds(Point.Empty);

    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
        }

        bitmap.Save(fileName, ImageFormat.Png);
    }
}
catch (Exception exc)
{
    MessageBox.Show(LanguageMessages.MsgTextErrorScreenDump + 
        Utilities.DoubleNewLine() + exc.ToString(), 
        LanguageMessages.MsgCaptionErrorScreenDump, 
        MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
    this.Show(this.Owner);
}

我尝试了什么: 隐藏表单后添加以下内容,这没有任何区别:

  • 睡眠(1000);
  • this.Repaint();
  • this.Invalidate();

我的问题是: 为什么this.Hide没有真正隐藏表单,从而阻止它与上面的代码一起被包含在屏幕转储中?

1 个答案:

答案 0 :(得分:1)

你可以使用计时器作弊,这会给你足够的时间"隐藏表格:

private System.Windows.Forms.Timer hideTimer = null;

一整秒似乎就足够了:

this.Hide();
hideTimer = new System.Windows.Forms.Timer() { Interval = 1000 };
hideTimer.Tick += (ts, te) => {
  hideTimer.Stop();
  hideTimer.Dispose();
  Rectangle bounds = Screen.GetBounds(this);
  using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) {
    using (Graphics g = Graphics.FromImage(bitmap)) {
      g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
    }
    bitmap.Save(fileName, ImageFormat.Png);
  }
  this.Show(this.Owner);
};
hideTimer.Start();
相关问题