tags:

views:

196

answers:

2

I want to do basic WPF graphics, i.e. rectangles, lines, circles and text.

When should I use Drawing and when should I use a DrawingVisual?

I have some code that uses Drawing and I render those to a DrawingImage and display that in an image control. Is this the right way? I could not see how to add text to a drawing. I had trouble positioning it too. Should I be rendering to a Canvas?

I have some code that uses DrawingVisual and writes to a DrawingContext. That is like WinForms. Is this the recommended way?

Do you have any high level advice on which APIs to use for basic graphics and labels? Will they options work on Silver Light and Desktop?

+1  A: 

You'll most likely want to just render to a Canvas. If you add "shapes" to a canvas, WPF will handle all of the drawing for you.

For details, see Shapes and Basic Drawing in WPF.

Reed Copsey
Thanks, I will try out Shapes and Canvas. Can I position text on the Canvas too? The application needs to draw about 1000 basic shapes, e.g .lines, circles and rectangles and some labels. It is a custom graph output. I read that visuals were good for speed, but do they work in silver light? I am looking for a way to render the graphics and text together with similar position accuracy to the drawing in winforms.
@peterhu: Text works fine for this - in my exp (WPF) this works great for this number of items.
Reed Copsey
+1  A: 

You mention in your comment that you're actually implementing scatter plots or similar graphs. Typically in WPF this is created by templating the existing controls, like a listview. It sounds counterintuitive, but this is far easier and more powerful than drawing your own.

This article by Charles Petzhold shows a scatter plot implemented in this way and goes into great detail about how to make it performant to 10,000+ data points.

Jay