Posted by: Zeeshan Amjad | July 26, 2009

DrawingVisual class in WPF


This lightweight class is used to render the vector graphics, images, shapes, text etc on the screen. It is important that this class doesn’t provide any feature layout, input handling, focus and event handling to improve the performance.

Just like the ContainerVisual class, this class also needs container to host it. Container of this class has to be a object of FrameworkElement or its subclass. We can use Window as a container of this, because Window is also inherited indirectly from FrameworkElement class. We get a drawing context from RenderOpen function to draw something on the Visual. Here is an example to show this.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: using System.Windows.Input;
  5: using System.Globalization;
  6: 
  7: public class MyWindow : Window
  8: {
  9:     private DrawingVisual dv;
 10: 
 11:     public MyWindow()
 12:     {
 13:         Title = "DrawingVisual Class";
 14:         Width = 400;
 15:         Height = 300;
 16:         WindowStartupLocation = WindowStartupLocation.CenterScreen;
 17: 
 18:         dv = new DrawingVisual();
 19: 
 20:         DrawingContext dc = dv.RenderOpen();
 21: 
 22:         Rect rect = new Rect(new Point(75, 75), new Point(25, 50));
 23:         dc.DrawRectangle(Brushes.Yellow, new Pen(Brushes.Navy, 2), rect);
 24:         dc.DrawEllipse(Brushes.Wheat, new Pen(Brushes.Brown, 3), new Point(200, 150), 25, 50);
 25:         FormattedText ft = new FormattedText("Hello from DrawingVisual",
 26:             CultureInfo.GetCultureInfo("en-us"),
 27:             FlowDirection.LeftToRight,
 28:             new Typeface("Verdana"), 24, Brushes.White);
 29: 
 30:         dc.DrawText(ft, new Point(0, 0));
 31:         dc.Close();
 32: 
 33:         AddVisualChild(dv);
 34:     }
 35: 
 36:     protected override int VisualChildrenCount
 37:     {
 38:         get { return 1; }
 39:     }
 40: 
 41:     protected override Visual GetVisualChild(int index)
 42:     {
 43:         if (index > 0)
 44:         {
 45:             throw new ArgumentOutOfRangeException("index");
 46:         }
 47: 
 48:         return dv;
 49:     }
 50: }
 51: 
 52: public class wpf
 53: {
 54:     [STAThread]
 55:     public static void Main()
 56:     {
 57:         MyWindow win = new MyWindow();
 58: 
 59:         Application app = new Application();
 60:         app.Run(win);
 61:     }
 62: }
 63: 

This program draws one ellipse, one rectangle and writes one string “Hello from DrawingVisual” on the screen. Here is the output of this program.

wpf_19

Advertisements

Responses

  1. Nice example. One minor point, I think it is better to handle “DrawingContext dc = dv.RenderOpen(); … dc.Close();” with “using()”. Do you know how to display a control, e.g. ComboBox in the DrawingContext?


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: