【问题标题】:Why does a Hidden Window appear when taking a screenshot in WPF using C#?为什么使用 C# 在 WPF 中截屏时会出现隐藏窗口?
【发布时间】:2014-09-09 19:38:37
【问题描述】:

我有一个WPF 应用程序,它可以帮助我截取整个屏幕。

该应用程序包含一个带有按钮的小窗口,如果我单击该按钮,则会捕获屏幕截图并将其保存在我的驱动器上。

我想在捕获之前隐藏这个小窗口,所以它不会包含在捕获中,这是我尝试过的:

StartSnipping = new RelayCommand(() =>
                 {
                   // I hide it here !!
                   WindowVisibility = Visibility.Hidden;                              

                   //Than I take the screen shot
                   var screen = Screen.PrimaryScreen;
                   var bitmap = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
                   using (var graphics = Graphics.FromImage(bitmap))
                   {
                     graphics.CopyFromScreen(new Point(screen.Bounds.Left, 
                                                       screen.Bounds.Top),
                                             new Point(0, 0), 
                                             screen.Bounds.Size);
                   }

                   //I save the screen shot
                   bitmap.Save(@"C:/Users/Aymen/Desktop/test.png", ImageFormat.Png);
                 }); 

现在,假设这条线:

WindowVisibility = Visibility.Hidden;

确实有效,好吧,我确信这一点,因为在所有该方法完成运行之后,窗口确实被隐藏了。

假设屏幕截图已成功拍摄并保存并且仍然包含该窗口,该窗口应该在屏幕截图前 1 秒被隐藏,我的问题是:

为什么这个隐藏的窗口仍然出现在屏幕截图中??我应该怎么做才能让它不出现在屏幕截图中??

【问题讨论】:

  • //give it some time... Thread.Sleep()... - 请学会正确地做多线程。
  • @HighCore thx 的评论,这是我添加的一个绝望的东西,虽然现在删除了
  • 很难猜到“WindowVisibility”可能意味着什么,没有同名的属性。 Windows 版本也很重要,Windows 8 对完全透明窗口的处理方式不同,并且您无法在早期版本中使用 CopyFromScreen() 捕获分层窗口。
  • @HansPassant WindowVisibilityViewModel 中的一个属性,Window 的 Visibility 属性绑定到该属性

标签: c# .net wpf windows window


【解决方案1】:

不确定,但似乎使用:

WindowVisibility = Visibility.Hidden; 

截屏时不显示窗口,我不得不使用 .Hide() 方法隐藏窗口:

Application.Current.MainWindow.Hide();

效果很好,bust 仍然没有任何解释为什么Visibility.Hidden 让窗口出现在屏幕截图中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2011-05-19
    • 2013-12-25
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多