【发布时间】: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 吗?我相信这就是你所追求的,它是自动的。