1. Winform vs WPF (for drawing visual)

Typically, you “draw” in WPF in a completely different manner.

In Windows Forms/GDI, the graphics API is an immediate mode graphics API. Each time the window is refreshed/invalidated, you explicitly draw the contents using Graphics.

In WPF, however, things work differently. You rarely ever directly draw - instead, it’s a retained mode graphics API. You tell WPF where you want the objects, and it takes care of the drawing for you.

The best way to think of it is, in Windows Forms, you’d say “Draw a line from X1 to Y1. Then draw a line from X2 to Y2. Then …”. And you repeat this every time you need to “redraw” since the screen is invalidated.

In WPF, instead, you say “I want a line from X1 to Y1. I want a line from X2 to Y2.” WPF then decides when and how to draw it for you.

This is done by placing the shapes on a Canvas, and then letting WPF do all of the hard work.

总之,wpf负责了具体的绘制流程,没有Winform中的闪屏现象

2. DrawingVisual vs DrawingContext

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Media.Visual
        System.Windows.Media.ContainerVisual
          System.Windows.Media.DrawingVisual

DrawingVisual is a visual object that can be used to render vector graphics on the screen. DrawingContext drawingContext = drawingVisual.RenderOpen();,drawingVisual获取 drawingContext就可以进行具体的绘制了。

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.Media.DrawingContext

DrawingContext describes visual content using draw, push, and pop commands. 描述具体的描述绘制对象(画线、圈、矩形、圆角矩形、文字、图形图片视频

最佳的使用方法是从Canvas继承,如下:

public class MyCanvas : Canvas
{
    protected override void OnRender(DrawingContext dc)
    {
        base.OnRender(dc);
        //使用dc进行自定义绘制
    }
}