开发者

VisualBrush from Window retains image of previous VisualBrush

开发者 https://www.devze.com 2023-03-11 23:55 出处:网络
Using VisualBrush, I am taking snapshots of a Window that contains a TabControl. Snapshot #1: Switch to \"Dos\" (not Microsoft), snapshot #2:

Using VisualBrush, I am taking snapshots of a Window that contains a TabControl.

Snapshot #1:

VisualBrush from Window retains image of previous VisualBrush

Switch to "Dos" (not Microsoft), snapshot #2:

VisualBrush from Window retains image of previous VisualBrush

If I just take a picture of the TabControl or the DockPanel, everything works fine, this problem is particular to taking a picture of the Window. Help!!

Code behind:

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 Heade开发者_JAVA百科r="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>


I found that I was able to get it to work by commenting out the part where I make a VisualBrush from the Window.

//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();
//}

and to call the element directly in renderTargetBitmap.Redner

renderTargetBitmap.Render(element);

though now I'm not sure why I ended up using DrawRectangle and VisualBrush when I could have just rendered the element directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号