Why does use of pens with dash patterns cause huge (!) performance degredation in WPF custom 2D drawing?

后端 未结 4 1772
遇见更好的自我
遇见更好的自我 2021-01-03 08:34

Hope anyone can shed light on this so I can use pens with dash patterns?

I am writing a scrollable chart (a Panel inside ScrollViewer that

相关标签:
4条回答
  • 2021-01-03 09:07

    Most likely this is because this particular draw operation isn't something that can be delegated to the video card, which would force composition in memory and blitting to the video card.

    0 讨论(0)
  • 2021-01-03 09:17

    Here's a possible workaround - if you're only drawing horizontal and/or vertical lines you could try creating your Pen with a checker pattern DrawingBrush such as:

      <Pen x:Key="PenUsingDashPatterns" Thickness="1">
            <Pen.Brush>
                <DrawingBrush TileMode="Tile" 
                              Viewport="0 0 6 6" ViewportUnits="Absolute">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing Brush="Black">
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <RectangleGeometry  Rect="0 0 3 3"/>
                                    <RectangleGeometry  Rect="3 3 3 3"/>
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Pen.Brush>
        </Pen>   
    

    Alternatively, you could use different brushes for verical and horizontal lines, or, possibly, an ImageBrush for better performance.

    0 讨论(0)
  • 2021-01-03 09:21

    You should use Perforator to dig deeper into the performance issue. Here's a link to the MSDN site talking about various WPF performance tools. Perforator would probably be the tool that will help you the most, specially in determining if the lines are being drawn using the software renderer (which would be the greatest factor in giving you such bad performance).

    If the problem is that they are being drawn in software, you might have to write your own ShaderEffect, but that will probably get tricky fast unless you are familiar with HLSL.

    0 讨论(0)
  • 2021-01-03 09:22

    Are the pens getting frozen? Freezing drawing objects helps performance a lot.

    You could set up a Loaded handler and debug to see if your pens are frozen. If not, Call the Pen.Freeze() button manually on them.

    Note that freeze also makes the pens read-only... you will be unable to modify them after you freeze them.

    0 讨论(0)
提交回复
热议问题