我有两种方法可以在屏幕上绘制一个旋转的矩形。
RenderMethod1
使用DrawingVisual
private static void RenderMethod1(DrawingContext dc) {
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext context = drawingVisual.RenderOpen()) {
Rect rect = new Rect(new System.Windows.Point(100, 100), new System.Windows.Size(320, 80));
context.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);
}
drawingVisual.Transform = new RotateTransform(30, 100, 100);
dc.DrawDrawing(drawingVisual.Drawing);
}
RenderMethod2
使用DrawingGroup
private static void RenderMethod2(DrawingContext dc) {
DrawingGroup group = new DrawingGroup();
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext context = drawingVisual.RenderOpen()) {
Rect rect = new Rect(new System.Windows.Point(100, 100), new System.Windows.Size(320, 80));
context.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);
}
group.Children.Add(drawingVisual.Drawing);
group.Transform = new RotateTransform(30, 100, 100);
group.Freeze();
dc.DrawDrawing(group);
}
输出如下:
RenderMethod1
RenderMethod2
正如您所看到的,RenderMethod1
和RenderMethod2
输出应该完全相同,但它们不是。在RenderMethod1
中我有什么问题吗?
提前感谢您的帮助,
答案 0 :(得分:0)
我终于通过更改RenderMethod1来解决问题,并按预期工作。
private static void RenderMethod1(DrawingContext dc) {
DrawingGroup drawingVisual = new DrawingGroup();
using (DrawingContext context = drawingVisual.Open()) {
Rect rect = new Rect(new System.Windows.Point(100, 100), new System.Windows.Size(320, 80));
context.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect);
}
drawingVisual.Transform = new RotateTransform(30, 100, 100);
dc.DrawDrawing(drawingVisual);
}