【问题标题】:wpf how to make a framwork element store a drawing visual as a bit map and display that on render?wpf 如何使框架元素将绘图视觉对象存储为位图并在渲染时显示?
【发布时间】:2013-12-25 03:03:47
【问题描述】:

我想让我的框架元素 signalgraph 将信号(DrawingVisual)显示为位图,然后在 WPF 通过渲染通道时渲染位图。我猜我覆盖了 FrameworkElement 的 onRender,但我不确定。我只是想尝试将所有内容都转换为位图,因为我认为程序渲染速度很慢,因为行数太多,将其保存为位图有望提高性能。

作为测试,我尝试将 SkyBlue Rectangle 添加到框架元素,但没有显示。

我创建了一个继承自 Shape 的矩形,它继承自 Visual。然后我使用 RenderTargetBitmap 类来渲染视觉,然后将 rendertargetbitmap 的输出渲染设置为 Image 的源。图像是一个框架元素,所以我只是将它添加到视觉集合中,希望它会显示,但什么都没有出现。

    Rectangle myRect = new Rectangle();
    myRect.Stroke = System.Windows.Media.Brushes.Black;
    myRect.Fill = System.Windows.Media.Brushes.SkyBlue;
    myRect.HorizontalAlignment = HorizontalAlignment.Left;
    myRect.VerticalAlignment = VerticalAlignment.Center;
    myRect.Height = 200;
    myRect.Width = 200;
    BitmapImage = new RenderTargetBitmap(1000, 1000, 96, 96, PixelFormats.Default);
    BitmapImage.Render(myRect);
    GraphImage = new Image();
    GraphImage.Source = BitmapImage;
    visuals.Add(GraphImage); //visuals is a VisualCollection

不知道我做错了什么,但我可能会尝试将画布上绘制的线条保存到位图的任何想法或替代方案将不胜感激。

【问题讨论】:

  • 您查看过BitmapCache 吗?我相信这就是你所追求的,它是自动的。

标签: c# wpf


【解决方案1】:

如果性能是主要因素,我建议您不要使用 WPF 形状,而是尝试DrawingVisual,它的性能与 WPF 形状相比非常好。

它非常易于使用和集成到项目中。只需要create one VisulHost say Canvas which will hold VisualCollection of all drawing visual,你要画。覆盖这两个 FrameworkElement 成员 - GetVisualChildVisualChildrenCount

您可以在 MSDN 上阅读更多信息 - Using DrawingVisual Objects

也可以参考这个以获得已经可用的工作解决方案 - WPF DrawTools

【讨论】:

    猜你喜欢
    • 2016-07-23
    • 2011-09-06
    • 2010-12-25
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    相关资源
    最近更新 更多