Performance difference between Path and Line? And, Bug in Path?

Hi all,

I'm creating an app that draws a lot of single-pixel straight lines. Would a GeometryGroup of LineGeometry be a more efficient choice than a List of Line shapes Or, is the performance difference negliable to really matter

Two considerations may affect the question. One, all lines may be subject to frequent RenderTransforms. And two, it appears that when Path.Data is set to the GeometryGroup of LineGeometry the SnapsToDevicePixels is not being applied consistently. In the following code example you may find that the first LineGeometry, assigned to a Path object with SnapsToDevicePixels="true", renders with anti-aliasing. Is the Path object working as intended

Thanks,

Rana Ian


--- XAML Example ---

<StackPanel xmlns="http://schemas.microsoft.com/winfx/avalon/2005">

<Path Stroke="Black" StrokeThickness="1" SnapsToDevicePixels="true">
 <Path.Data>
  <GeometryGroup FillRule="Nonzero" >
   <LineGeometry StartPoint="100,50" EndPoint="200,50"  />
   <LineGeometry StartPoint="100,100" EndPoint="200,100" />
  </GeometryGroup>
 </Path.Data>
</Path>

<Line
    X1="200" Y1="50"
    X2="300" Y2="50"
    Stroke="Red"
    SnapsToDevicePixels="true"
    StrokeThickness="1" />

<Line
    X1="200" Y1="50"
    X2="300" Y2="50"
    Stroke="Red"
    SnapsToDevicePixels="true"
    StrokeThickness="1" />

</StackPanel>



Answer this question

Performance difference between Path and Line? And, Bug in Path?

  • rsavas

    Hi,

    In general you should see better performance by having your lines inside a GeometryGroup, since this would require traversing less elements in the tree. You might get even better performance if you use a GeometryDrawing, since they are even lighter weight than a Path (which is a UIElement).

    The second question you ask is a little more complicated...

    There isn't actually any way to request a 1-pixel wide line in WPF. What you're really asking for when you say "StrokeThickness='1'" is a line which is 1/96th of an inch... on a machine with a correctly set DPI, this line will always appear anti-aliased at the same effective size.

    The "SnapsToDevicePixels" option only affects the layout bounds, and not the rendering of the line itself, so in general won't do what you expect. A diagonal line, for example, will always be anti-aliased.

    The good news is that I believe you can get the behavior you want using the "DrawingContext.PushGuidelineSet()" function. Unfortunately, you'll need to call this from code (you can't do it from XAML), but it does enable you to specify that horizontal or vertical lines should snap towards the nearest pixel.

    Just post again with follow up questions if you need more help.

    Thanks,

  • Tomas Galvez

    Alexander,

       Thanks for the helpful reply. I'll experiment with GeometryDrawing and DrawingVisual, too. I'm very keen on consistently drawing 1 pixel-wide lines without anti-aliasing. 

       I wasn't able to find "DrawingContext.PushGuidelineSet()". Are you using a MS internal version of WPF I don't see a static method, or instance method named "PushGuidelineSet" in DrawingContext, or any class, for that matter. I'm using the September 14, 2005 SDK and documentation. Is there another method perhaps

    Great thanks for the help,

    Rana
       


  • Duke Voldemar

    A Geometry is "lighter" than a Shape, so usually it is better to use geometries than shapes.



  • Performance difference between Path and Line? And, Bug in Path?