0
votes

If I wanted to create a form with a drawing area in a traditional Windows form, I would extend the panel class and then override the onPaint method to do my custom drawing, like so:

public class SpecialPanel : System.Windows.Forms.Panel
{
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics G = e.Graphics;
        G.DrawEllipse(new Pen(Color.Red), 50, 80, 50, 10);

    }
}

But in a WPF if I attempt to extend the canvas class there is no onPaint method to override

public class SpecialCanvas : Canvas //Error no OnPaint method
{
    public override OnPaint(PaintEventArgs e)
    {

    }
}

If I wanted to draw to a Canvas I would do so in a way similar to this:

        Canvas.SetLeft(shape.shape, r.Next(1, 1150));
        Canvas.SetTop(shape.shape, r.Next(1, 500));
        SolidColorBrush b = new SolidColorBrush(Windows.UI.Color.FromArgb(255, (byte)r.Next(1, 255), (byte)r.Next(1, 255), (byte)r.Next(1, 255)));
        shape.shape.Fill = b;
        shape.shape.Stroke = b;
        Cann.Children.Add(shape.shape);

Where Cann is an a canvas object and shape.shape is a Rectangle.

My question is, what are the fundamental differences between these methods of drawing to a form?

1
A WPF Canvas is a Container control, like Grid and StackPanel. Drawing pixels happens several levels lower, in FrameworkElement. But you probably don't want to bother with that. Use Shapes. - Henk Holterman
WPF and WinForms are two completely different frameworks. You should get a book about WPF, e.g. Adam Nathan's WPF Unleashed. It explains all the fundamental differences in detail. - Clemens

1 Answers

3
votes

Fundamental differences between WPF and GDI/GDI+

The Windows Presentation Foundation (WPF) is fundamentally different from the Graphics Device Interface (GDI) and GDI+, requiring that many aspects of programming be approached in a different way. This topic briefly outlines the major differences.

Windows

Applications built using the GDI/GDI+ API use many windows, and reside under a parent window (MDI). Applications built in WPF have one window.

Unit of Measure

Applications built with a GDI/GDI+ API use the hardware pixel as the unit of measure. In these applications, as the resolution of the display device increases, the resulting image decreases. Applications built in WPF use the device-independent unit (1/96-inch) as the unit of measure. When the DPI of the system is 96, the two are equivalent.

Control Positioning

Applications built with GDI+ use absolute positioning. When the parent is resized the child elements do not get resized along with it. Applications built in WPF can use absolute, dynamic or data-bound positioning. With either absolute or dynamic positioning the controls are positioned relative to the parent element.

Image Basis

Images formed in GDI/GDI+ are pixel-based, raster images. Those formed in WPF can be scalable, vector images.

Rendering Engine

Both GDI and GDI+ are built on Win32. GDI is based on the concept of a device context, where applications obtain handles to the device context, and use the handles in order to interact with the device. GDI+ is a wrapper around GDI that creates a C++ Graphics object. WPF, on the other hand, is built on DirectX which means that it can take advantage of hardware acceleration when performing its drawing operations.

Rendering Mode

With GDI and GDI+, rendering uses immediate rendering: the application repaints the area that becomes invalidated. In WPF, rendering uses retained rendering: the application keeps track of the drawing information but the system performs the painting.

Painting

With GDI and GDI+, clipping is used to determine the bounds of the area that has become invalidated and needs to be painted. In WPF, painting is performed from back to front and components paint over each other.

Pens and Brushes

GDI and GDI+ use the concept of a current brush and a current pen. In WPF, the brush and pen must be passed with each drawing call.

Paint Region Optimization

Paint region optimization is an important part of painting with GDI or GDI+. In WPF it is not considered.

Events

GDI and GDI+ use events and event handler delegates using subscription/notification. WPF uses bubbling, tunneling, and direct event notification, and the event travels up and down the VisualTree.

Video and Audio

Direct support for video and audio is not provided by GDI+ or Windows Forms, but must be obtained through a media player like Windows Media Player. WPF supports video and audio directly.

source: http://www.leadtools.com/help/leadtools/v18/dh/to/leadtools.topics~leadtools.topics.differencesbetweengdiandwpf.html