简体   繁体   中英

How should i draw text and shapes in wpf and C#?

I want to do basic WPF graphics, ie 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM