本文介绍了绘图组与绘图视觉变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种方法可以在屏幕上绘制一个旋转的矩形.

I have two methods that draw a rotated rectangle on the screen.

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

如您所见,RenderMethod1RenderMethod2 输出应该完全相同,但实际上并非如此.我在 RenderMethod1 中做错了什么吗?

As you can see RenderMethod1 and RenderMethod2 outputs are supposed to be exactly the same but they are not. Is there anything I am doing wrong in RenderMethod1?

提前感谢您的帮助

推荐答案

我终于通过如下更改 RenderMethod1 解决了这个问题,它按预期工作.

I finally got around the problem by changing RenderMethod1 as follow and it works as expected.

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);
}

这篇关于绘图组与绘图视觉变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 19:05