首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >窗口中的VisualBrush保留以前VisualBrush的图像

窗口中的VisualBrush保留以前VisualBrush的图像
EN

Stack Overflow用户
提问于 2011-06-09 18:18:51
回答 1查看 687关注 0票数 3

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

快照#1:

切换到"Dos“(不是微软),快照#2:

如果我只拍了一张TabControl或DockPanel的照片,一切都很好,这个问题对于拍摄窗口的照片来说是很特殊的。救命!!

代码背后:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-09 18:31:35

我发现我能够通过注释掉我从窗口制作VisualBrush的部分来使它工作起来。

代码语言:javascript
复制
//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中直接调用元素

代码语言:javascript
复制
renderTargetBitmap.Render(element);

尽管现在我不知道为什么我会使用DrawRectangle和VisualBrush,而我本来可以直接呈现这个元素。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6297386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档