窗口中的VisualBrush保留了先前VisualBrush的图像。

4
使用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>

我需要拍摄“窗口”的照片有一个很好的理由。那是因为我们正在使用带有框架的自定义窗口,这个框架也应该被捕捉到。 - tofutim
1个回答

1

我发现通过注释掉从窗口创建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.Render中调用该元素。

renderTargetBitmap.Render(element);

虽然现在我不确定为什么我最终使用了DrawRectangle和VisualBrush,而不是直接渲染元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接