Use StreamGeometry in WPF to improve performance ., Wpfstreamgeometry
We are currently using WPF to develop a stock K-line chart. performance considerations are the biggest aspect. Each column is 5 pixels wide, and the interval between the column and the column is 3 pixels.
A 1920*1080 resolution screen is bound to draw more than 200 columns. If the selected drawing scheme is not advanced, it will have a great impact on the performance of the chart.
Any graph consists of a straight line and a curve. In WPF, the lowest level of drawing is Geometry.
In WPF, The DrawingContext method is used to draw a straight line. A LineGeometry object is created internally and a rectangle is drawn. A RectagleGeometry object is created internally.
Therefore, it is unscientific to use the traditional DrawLine and DrawRectangle to draw these columns. Because each column contains a rectangle and a straight line.
Suppose we Plot 200 columns, and instantiate 200 LineGeometry objects and 200 RectangleGeometry objects each time.
While dragging, the screen delay is very obvious when the Geometry object is instantiated and recycled constantly.
Is there a scheme to reduce the generation of objects. StreamGeometry! StreamGeometry is very efficient because it directly stores the data drawn by byte streams.
StreamGeometry works in a way similar to a laser printer. It can switch the starting coordinates of the next drawing at will.
After carefully analyzing the K-line chart, we will find that the K-line chart is composed of two colored columns. Because DrawingContext can only submit one color fill and draw edge commands at a time. Therefore, it must be drawn at least twice.
Using two StreamGeometry objects, You can first draw red columns and then draw all columns in green.