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

发布于 2024-11-14 14:41:36 字数 3011 浏览 2 评论 0原文

我使用 VisualBrush 拍摄包含 TabControl 的窗口的快照。

快照 #1:

在此处输入图像描述

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

在此处输入图像描述

如果我只拍摄 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>

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

Snapshot #1:

enter image description here

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

enter image description here

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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

书信已泛黄 2024-11-21 14:41:36

我发现我可以通过注释掉从窗口创建 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。

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.

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