更新前截图
我想捕获屏幕图像,但是我的应用程序中有一些我不希望出现在屏幕截图中的 wpf 控件。 使用下面的代码,隐藏的控件有时仍会出现在屏幕截图中。 我如何确保这种情况不会发生?
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);
}
I want to capture an image of the screen, however there are some wpf controls in my application that I do not want to appear in the screenshot. Using the code below, the hidden controls will sometimes still appear in the screenshot. How do I ensure that this doesn't happen?
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在调度程序上使用低优先级,但这仍然不能保证(
另外,根据我的理解,WPF 在 UI 线程空闲(或长时间阻塞)之前不会启动渲染过程,因此您可能需要将调度阻塞和放置在 UI 线程中。 实际后台线程中的快照代码(或让调度程序异步调用快照代码)。 然后在快照线程完成后恢复按钮可见性。
Try using a low priority on the dispatcher, it still isn't a guarantee though (source).
Also, from my understanding WPF doesn't start the render process until the UI thread is idle (or lengthily blocked), so you may want to place the dispatch blocking & snapshot code in an actual background thread (or have the dispatcher asynchronously invoke the snapshot code). Then restore the button visibility once that snapshot thread is complete.
您是否尝试过将
DispatcherPriority
设置为Normal
而不是Background
?Have you tried setting the
DispatcherPriority
toNormal
rather thanBackground
?