繁体   English   中英

wpf如何使框架元素将图形视觉存储为位图并在渲染时显示?

[英]wpf how to make a framwork element store a drawing visual as a bit map and display that on render?

我想让我的框架元素signalgraph将信号(DrawingVisual)显示为位图,然后在WPF经过渲染过程时渲染位图。 我猜我为FrameworkElement覆盖了onRender,但是我不确定。 我只想尝试将所有内容转换为位图,因为我认为程序会因为行数过多而呈现缓慢的状态,将其保存到位图有望改善性能。

作为测试,我尝试将SkyBlue矩形添加到framework元素,但未显示。

我创建一个从Shape继承的矩形,该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

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

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

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

您可以在MSDN- Using DrawingVisual Objects上阅读有关此内容的更多信息。

也可以参考已存在的工作解决方案-WPF DrawTools

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM