Taurius is a coder with many tech interests. He enjoys creating helpful tutorials.
Every platform has a canvas, and that is its own kind of canvas. Every canvas feature is tricky, but at the same time quite useful.
For this tutorial you will need to use visual studio and establish WPF project, this works on both .NET framework and .NET CORE.
In the xaml, you only need to establish a canvas element. For the purpose of showcasing, this project will have three buttons for three different shapes that you can draw on canvas:
We will be drawing line, circle (ellipse) and square (rectangular).
Now that we have the canvas, we can go to the c# and we will start with the line
As you can see, you first need to construct the Line object, afterwards you need to set some properties to the object.
The Stroke property sets the color of the line, the next for properties will declare the start and end points for the line. X1 and Y1 coordinate set, will be the point where the line begins then X2 and Y2 will be the end point.
Once you have those properties set, you need to add the object to the canvas object Children list, You can add as many as you want.
The second shape is an ellipse or in this case — a circle.
Everything is very similar to the line, one thing to note here is that you need to set height and width the same to get a circle
The last shape is the rectangular
In the last shape we add one additional parameter, which is the margin. Normally, when you draw a shape it will start at (0;0) coordinate, so if you want to put it somewhere else in the canvas you need to set a margin. As you can see, you have to use Thickness to set margin in WPF.
This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.