Confessions of a .NET Developer!

WPF Tutorial : Drawing Visual

As described in the MSDN, is a lightweight drawing class to render shapes, images, videos, text etc. It’s so lightweight that it needs a container to hold it, also the fact that it doesn’t provide layout or hit-testing or event-handling.

So for holding our DrawingVisual, we will use a which will provide layout, hit-testing and event-handling.

Lets get started by creating a class DrawIt which inherits FrameworkElement class. In this class we will create two rectangles using DrawingVisual class.

    class DrawIt:FrameworkElement
        VisualCollection visuals;

        public DrawIt()
            visuals = new VisualCollection(this);

            this.Loaded += new RoutedEventHandler(DrawIt_Loaded);

        void DrawIt_Loaded(object sender, RoutedEventArgs e)
            int x = 0;
            for (int i = 0; i <= 1; i++)
                DrawingVisual visual = new DrawingVisual();
                using (DrawingContext dc = visual.RenderOpen())
                    dc.DrawRectangle(Brushes.Red, new Pen(Brushes.Black, 2),
                        new Rect(new Point(0 + x, 0), new Size(40, 40)));
                x += 60;

        protected override Visual GetVisualChild(int index)
            return visuals[index];

        protected override int VisualChildrenCount
                return visuals.Count;

Explanation :

For this class, we have defined visuals of type VisualCollection. VisualCollection will keep the number of visual objects that we have drawn so far.
In the LoadedEvent, I am running a for-loop to draw 2 rectangles. In that, a DrawingVisual object is created for every loop made. The RenderOpen() method of DrawingVisual will return an object of type DrawingContext which is used to render the content of the visual. This line is straight from MSDN: “When you use a DrawingContext object’s draw commands, you are actually storing a set of rendering instructions that will later be used by the graphics system; you are not drawing to the screen in real-time”. This will greatly improve the performance.
Then there are two overrides which will be used by our container to manage the collection of the visual objects.

Next is to display our visuals in a Window. Here is the XAML of our window.

<Window x:Class="LearnDrawing.Window1"
    Title="Window1" Height="300" Width="300">
        <local:DrawIt Width="100" Height="100"/>

And the window will display this:



I would like you to read these articles too which helped me a lot:

That’s it. Hope it helped you. Happy coding!


May 19, 2011 - Posted by | WPF

1 Comment »

  1. super helpful

    Comment by HappyPotato | April 20, 2016 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: