1

画面の画像をキャプチャしたいのですが、スクリーンショットに表示したくないいくつかの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);
}
4

2 に答える 2

1

ディスパッチャで低い優先度を使用してみてください。ただし、それでも保証はありません(ソース)。

また、私の理解では、UIスレッドがアイドル状態になる(または長時間ブロックされる)までWPFはレンダリングプロセスを開始しないため、ディスパッチブロックとスナップショットコードを実際のバックグラウンドスレッドに配置する(またはディスパッチャーに非同期で呼び出す)ことをお勧めします。スナップショットコード)。次に、スナップショットスレッドが完了したら、ボタンの表示を復元します。

于 2009-08-12T01:04:27.183 に答える
0

ではなくに設定してみましDispatcherPriorityたか?NormalBackground

于 2009-07-22T15:28:41.847 に答える