[英]wpf how to make a framwork element store a drawing visual as a bit map and display that on render?
I want to make my framework element, signalgraph, display the signal (a DrawingVisual) as a bitmap and then render the bitmap when WPF goes through a rendering pass. 我想让我的框架元素signalgraph将信号(DrawingVisual)显示为位图,然后在WPF经过渲染过程时渲染位图。 I'm guessing I override onRender for the FrameworkElement, but I'm not sure. 我猜我为FrameworkElement覆盖了onRender,但是我不确定。 I just want to try converting everything to a bitmap because I think the program renders slow because there are too many lines, and saving it to a bit map would hopefully improve performance. 我只想尝试将所有内容转换为位图,因为我认为程序会因为行数过多而呈现缓慢的状态,将其保存到位图有望改善性能。
As a test, I tried to add a SkyBlue Rectangle to the framework element, but it is not displaying. 作为测试,我尝试将SkyBlue矩形添加到framework元素,但未显示。
I create a rectangle that inherits from Shape, which inherits from Visual. 我创建一个从Shape继承的矩形,该Shape从Visual继承。 I then use the RenderTargetBitmap class to render the visual, and then set the output render of the rendertargetbitmap as the source of an Image. 然后,我使用RenderTargetBitmap类渲染视觉效果,然后将rendertargetbitmap的输出渲染设置为Image的源。 Image is a framework element, so I then just add it to the visual collection in the hopes that it will display, but nothing appears. 图像是一个框架元素,因此我只是将其添加到视觉集合中,希望它可以显示,但是什么也没有出现。
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
Not sure what I'm doing wrong, but any ideas or alternatives that I might try to save the lines drawn on the canvas to a bit map would be appreciated. 不知道我在做什么错,但是我可能会尝试将在画布上绘制的线条保存到位图的任何想法或替代方案将不胜感激。
If performance is major factor, I would suggest you instead of going with WPF shapes, try DrawingVisual whose performance is very good compare to WPF shapes. 如果性能是主要因素,我建议您不要使用WPF形状,请尝试DrawingVisual,其性能与WPF形状相比非常好。
It's very easy to use and integrate to project. 它非常易于使用和集成到项目中。 Simply you need to create one VisulHost say Canvas which will hold VisualCollection of all drawing visual
, you want to draw. 只需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
。 Override few these two FrameworkElement members - GetVisualChild
and VisualChildrenCount
. 覆盖这两个FrameworkElement成员中的几个GetVisualChild
和VisualChildrenCount
。
You can read more about here on MSDN - Using DrawingVisual Objects . 您可以在MSDN- Using DrawingVisual Objects上阅读有关此内容的更多信息。
Also can refer to this for already available working solution - WPF DrawTools . 也可以参考已存在的工作解决方案-WPF DrawTools 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.