Bill Reiss
Bill Reiss

Reputation: 3460

DrawingVisual performance with Opacity=0

If I have a DrawingVisual in WPF with Opacity=0, is that enough for it not to be drawn? We have hundreds of DrawingVisuals on a Canvas, and are currently setting Opacity=0 on the visuals that are not to be displayed, and I wanted to make sure there is no rendering performance hit for rendering a DrawingVisual with Opacity=0.

UPDATE: I have discovered through testing that there IS overhead when Opacity=0, but since DrawingVisual doesn't have a Visibility property, I don't know how else you would tell it to not be displayed unless you actualy remove it from the visual tree, so any suggestions are welcome.

Upvotes: 4

Views: 3713

Answers (4)

eesh
eesh

Reputation: 1414

I solved a very similar problem by using a DrawingGroup and adding or removing Drawing objects from the DrawingGroup as they either needed to be displayed or hidden. The key is to organize your Drawing objects in such a way that they are easy to manage and to understand how to add and remove them from the DrawingGroup.

Remember that you want to add and remove the Drawing objects from the DrawingCollection exposed by the DrawingGroup.Children property. So use DrawingGroup.Children.Add() or the other DrawingCollection methods: Insert, Remove, RemoveAt, Clear. You will need to keep an external list of the Drawing objects you add/remove to the DrawingGroup to do this successfully.

I used this technique to great effect by drawing an Image (bitmap) into the first child in my instance of DrawingGroup and then adding and removing Drawing objects to this instance of DrawingGroup in order to layer polygons, paths, text, etc on top of the drawing.

I "draw" or "erase" on the image by adding or removing Drawing objects to the instance of the DrawingGroup. The DrawingGroup is treated as a single Drawing and so any scaling, panning, or other manipulations will affect all Drawing objects within the DrawingGroup.

Upvotes: 3

keft
keft

Reputation: 366

The most efficient seems to be setting the opacity in my tests. Another simple approach is to redraw the visuals that are affected.

using (DrawingContext dc = RenderOpen()) {} //Hide this visual

And then redraw when they become visible again.

Rendering a blank drawingcontext seems to be very quick. But if you have complicated visuals it could take time to rerender them when they become visible.

Upvotes: 1

Theo Zographos
Theo Zographos

Reputation:

Why not simply remove the visual from the visual children list? When it needs to be visible you add it back.

Upvotes: 0

Pop Catalin
Pop Catalin

Reputation: 62960

The best way to check would be to instead set the Visibility to Visibility.Colapsed, and see if there's any drawing performance differences.

Visibility.Colapsed ensures that the element is not visible but also that it will not participate in the Arrange, Measure and Render passes of the UI, while an element with Opacity=0 might participate in all passes.

Upvotes: 2

Related Questions