我想创建一个绘图应用程序。我有三个主要班级。
形状类中的方法:
public DrawingVisual Paint()
{
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
Pen pen = new Pen(new SolidColorBrush(Color), Stroke);
dc.DrawRectangle(null, pen, new Rect(0, 0, Width, Height));
}
return dv;
}
另一个类中的渲染方法:
private void Render(DrawingContext dc)
{
System.Windows.Media.Pen pen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 2);
// Shape is my basic class for all my shapes
foreach (Shape shape in shapes)
{
DrawingVisual visual = shape.Paint();
VisualBrush brush = new VisualBrush(visual);
dc.DrawRectangle(brush, null, new Rect(shape.X, shape.Y, shape.Width, shape.Height));
}
}
和OnRender方法:
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
dc.DrawImage(image, new Rect(0, 0, image.Width, image.Height));
System.Windows.Media.Pen pen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 2);
Render(dc);
}
但是DrawingVisual类不起作用。你能帮我吗?