#233 – An Example of Deriving from DrawingVisual Class
March 2, 2011 3 Comments
You can define a new class that inherits from the DrawingVisual class when you need a low-level control to draw one or more 2D objects.
Below is an example showing a simple implementation of a class derived from DrawingVisual that draws a couple of objects.
class EllipseAndRectangle : DrawingVisual { public EllipseAndRectangle() { using (DrawingContext dc = RenderOpen()) { // Black ellipse with blue border dc.DrawEllipse(Brushes.Black, new Pen(Brushes.Blue, 3), // Border new Point(120, 120), 20, 40); // Center & radius // Red rectangle with green border dc.DrawRectangle(Brushes.Red, new Pen(Brushes.Green, 4), // Border new Rect(new Point(10, 10), new Point(80, 80))); // Corners } } }
The RenderOpen method allows us to render content into the DrawingVisual object. RenderOpen returns a DrawingContext, which allows us to draw various kinds of 2D objects.
The DrawingContext will actually cache all commands for drawing the objects that we tell it to draw. This means that we only have to issue our drawing commands once–in the class’ constructor.
A working example of HOW you use this stuff might be nice, perhaps expand upon the notion of “once it is hosted on a UIElement”…..how exactly do you do that…..bearing in mind we may not be quite as clued up as you, which is why we perhaps read your blog…..
Post #234 shows how to host an object that derives from DrawingVisual – https://wpf.2000things.com/2011/03/03/234-getting-a-drawingvisual-object-rendered-in-a-window/
Thank you for responding to me. I looked at #234 prior to sending my query as it didn’t seem to answer my problem. But to rephrase the question, what I wanted to do was add an image to (for example) a Canvas that is part of a UserControl, but to do so in the “code behind” and not in the WPF. A canvas is a UIElement so presumable you can add to it’s children, but that is once you have instantiated a UIElement from the derived object….that was the missing link for me!
Anyway, no worries, I eventually found the problem which was to do with creating BitmapImages from a crop of a bitmap. The magic elixir here was set the CacheOption on the BitmapOption to BitmapCacheOption.OnLoad so that the bitmap image gets fully built before moving on to the next one, so my problem was the “lazy-loading” that WPF seems to prefer….
This is what I have now:
Bitmap cropped = new Bitmap(SrcRect.Width, SrcRect.Height);
cropped = lcdcharset.Clone(CropRect, lcdcharset.PixelFormat);
using (MemoryStream ms = new MemoryStream())
{
cropped.Save(ms, ImageFormat.Bmp);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad; //This is so the bi gets fully setup
bi.StreamSource = ms;
bi.EndInit();
ImageSrc[row,col] = bi;
}
Kind regards,
Colin