簡體   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