Window中的VisualBrush保留了以前VisualBrush的图像

时间:2011-06-09 18:18:51

标签: c# wpf visualbrush

使用VisualBrush,我正在拍摄一个包含TabControl的窗口的快照。

快照#1:

enter image description here

切换到“Dos”(不是Microsoft),快照#2:

enter image description here

如果我只是拍摄TabControl或DockPanel的照片,一切正常,这个问题特别适合拍摄窗口的照片。帮助!

代码背后:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfVisual
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var bitmapSource = this.TakeSnapshot(this);
            Snapshot.Source = bitmapSource;
        }

        public BitmapSource TakeSnapshot(FrameworkElement element)
        {
            if (element == null)
            {
                return null;
            }

            var width = Math.Ceiling(element.ActualWidth);
            var height = Math.Ceiling(element.ActualHeight);

            element.Measure(new Size(width, height));
            element.Arrange(new Rect(new Size(width, height)));

            var visual = new DrawingVisual();
            using (var dc = visual.RenderOpen())
            {
                var target = new VisualBrush(element);
                dc.DrawRectangle(target, null, new Rect(0, 0, width, height));
                dc.Close();
            }

            var renderTargetBitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);
            renderTargetBitmap.Render(visual);  // maybe here?

            return renderTargetBitmap;
        }

    }
}

的Xaml:

<Window x:Class="WpfVisual.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <TabControl x:Name="Tabs" DockPanel.Dock="Left" Width="200">
            <TabItem Header="Uno">
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                           FontSize="50" Foreground="Red">#1</TextBlock>
            </TabItem>
            <TabItem Header="Dos">
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"
                           FontSize="50" Foreground="Green">#2</TextBlock>
            </TabItem>
        </TabControl>
        <Button DockPanel.Dock="Top" Margin="10" FontSize="14" Click="Button_Click">Take a snapshot</Button>        
        <Image x:Name="Snapshot" Margin="10,0,10,10"></Image>
    </DockPanel>
</Window>

1 个答案:

答案 0 :(得分:0)

我发现我能够通过注释掉从窗口创建VisualBrush的部分来实现它。

//var visual = new DrawingVisual();
//using (var dc = visual.RenderOpen())
//{
//    var target = new VisualBrush(element);
//    dc.DrawRectangle(target, null, new Rect(0, 0, width, height));
//    dc.Close();
//}

并直接在renderTargetBitmap.Redner

中调用元素
renderTargetBitmap.Render(element);

虽然现在我不知道为什么我最终会使用DrawRectangle和VisualBrush直接渲染元素。