Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing between Drawing and Shape in WPF

Tags:

wpf

graphics

2d

I am not quite sure about the differences between the classes System.Windows.Media.Drawing and System.Windows.Shapes.Shape. They both expose functionality related to 2D graphics in WPF. When would you choose one in your WPF application, and when would you choose the other?

like image 307
user181813 Avatar asked Feb 02 '23 18:02

user181813


1 Answers

A Shape inherits from FrameworkElement and is therefore a high level object which provides features such as hit-testing, styling, layout and data binding. In contrast a Drawing does not inherit from FrameworkElemet and doesn't support any of these features. As the documentation mentions a Drawing is useful for lightweight visual objects. If you are creating a complex brush to use to paint areas or a background a DrawingBrush would be very efficient.

Drawings can combine text, video, images and Geometry objects (another light weight class) to create complex but very efficient and fast drawings.

In short a Drawing is a low-level alternative to a Shape. As for use cases, it depends.

  • If you have to animate or do any sort of binding you would use Shapes.
  • If you are creating brushes or complex clip arts/vector graphics you would probably use Drawings.
  • Also, if you draw things by overriding OnRender you would mostly use Geometries.

A Drawing is also Freezable and can thus be shared among threads (assuming it is frozen).

like image 141
Patrick Klug Avatar answered Feb 06 '23 10:02

Patrick Klug