更新前截图

发布于 2024-07-27 12:25:02 字数 1670 浏览 1 评论 0原文

我想捕获屏幕图像,但是我的应用程序中有一些我不希望出现在屏幕截图中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作妖 2024-08-03 12:25:02

尝试在调度程序上使用低优先级,但这仍然不能保证(

另外,根据我的理解,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.

念三年u 2024-08-03 12:25:02

您是否尝试过将 DispatcherPriority 设置为 Normal 而不是 Background

Have you tried setting the DispatcherPriority to Normal rather than Background?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文