C# 截图前更新
我想捕获屏幕的图像,但是我的应用程序中有一些wpf控件,我不想出现在屏幕截图中。使用下面的代码,隐藏控件有时仍会出现在屏幕截图中。我如何确保这不会发生C# 截图前更新,c#,.net,wpf,gdi+,screenshot,C#,.net,Wpf,Gdi+,Screenshot,我想捕获屏幕的图像,但是我的应用程序中有一些wpf控件,我不想出现在屏幕截图中。使用下面的代码,隐藏控件有时仍会出现在屏幕截图中。我如何确保这不会发生 Window window = new Window(); Button button = new Button(); void ShowWindow() { button.Content = "button"; button.ToolTip = "tooltip"; window.Content = button;
Window window = new Window();
Button button = new Button();
void ShowWindow()
{
button.Content = "button";
button.ToolTip = "tooltip";
window.Content = button;
window.Show();
button.Click += new RoutedEventHandler(button_Click);
}
void button_Click(object sender, RoutedEventArgs e)
{
//Hide some controls and all tooltips in the window
ToolTipService.SetIsEnabled(window, false);
button.Visibility = Visibility.Hidden;
//Block until wpf renders
Application.Current.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new System.Threading.ThreadStart(delegate { }));
//Take screenshot
Bitmap bmpScreenshot = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X,
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y,
0, 0,
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
bmpScreenshot.Save("Screenshot.png", ImageFormat.Png);
//Restore controls and tooltips
button.Visibility = Visibility.Visible;
ToolTipService.SetIsEnabled(window, true);
}
您是否尝试过将
DispatcherPriority
设置为Normal
而不是Background
?您是否尝试过将DispatcherPriority
设置为Normal
而不是Background
?尝试对dispatcher使用低优先级,但这仍然不能保证()
另外,据我所知,WPF在UI线程空闲(或长时间阻塞)之前不会启动渲染过程,因此您可能希望将调度阻塞和快照代码放置在实际的后台线程中(或让调度程序异步调用快照代码)。然后,在快照线程完成后恢复按钮可见性。尝试在调度程序上使用低优先级,但这仍然不能保证()
另外,据我所知,WPF在UI线程空闲(或长时间阻塞)之前不会启动渲染过程,因此您可能希望将调度阻塞和快照代码放置在实际的后台线程中(或让调度程序异步调用快照代码)。然后,在快照线程完成后恢复按钮可见性。或渲染,而不是普通/背景更改DispacherPriority似乎没有帮助。我相信这条线的工作原理是强制处理所有具有更高优先级的操作。因此,为了强制渲染,它需要比渲染具有更低的优先级。然而,即使将其更改为最低优先级也不能解决问题。这很有趣,也很公平。IIRC,WPF中的实际渲染是在另一个线程(而不是调度程序线程)中完成的,因此,调度程序完成并不意味着像素仍在屏幕上。我不确定你所在的上游是否有这些信息。。。抱歉:)或渲染,而不是普通/背景更改DispacherPriority似乎没有帮助。我相信这条线的工作原理是强制处理所有具有更高优先级的操作。因此,为了强制渲染,它需要比渲染具有更低的优先级。然而,即使将其更改为最低优先级也不能解决问题。这很有趣,也很公平。IIRC,WPF中的实际渲染是在另一个线程(而不是调度程序线程)中完成的,因此,调度程序完成并不意味着像素仍在屏幕上。我不确定你所在的上游是否有这些信息。。。抱歉:)谢谢你的链接。似乎无法判断是否已呈现对wpf控件的更改。这个答案非常有用。我想做的事是不可能的。最好的办法是尝试一些技巧,希望能够使渲染线程在捕获之前运行并完成渲染,尽管到目前为止我无法保证这一点。感谢链接。似乎无法判断是否已呈现对wpf控件的更改。这个答案非常有用。我想做的事是不可能的。最好的办法是尝试一些技巧,希望能够使渲染线程在捕获之前运行并完成渲染,尽管到目前为止,我无法保证这一点。