简体   繁体   English

将XAML形状绘制到DrawingContext吗?

[英]Draw XAML shapes to DrawingContext?

I have several common symbols that have been composed of WPF shape primatives (eg Rectangle, Line, Path etc). 我有一些由WPF形状基元组成的常见符号(例如Rectangle,Line,Path等)。 These symbols are currently stored in xaml files as user controls. 这些符号当前作为用户控件存储在xaml文件中。 For example: 例如:

<Canvas x:Name="Symbol" Width="100" Height="100">
    <Rectangle x:Name="Rectangle" Width="100" Height="100" Stretch="Fill">
        <Rectangle.Fill>
            <SolidColorBrush Color="Aqua"></SolidColorBrush>
        </Rectangle.Fill>
    </Rectangle>
</Canvas>

The above is a simple example but most of my symbols are more complex and composed of many different parts. 上面是一个简单的示例,但是我的大多数符号更加复杂,由许多不同的部分组成。 So, I was hoping to use these user controls to store the symbols and make them easy to manipulate. 因此,我希望使用这些用户控件来存储符号并使它们易于操作。

I am doing quite a bit of drawing in the OnRender method of a Canvas and would like to include some of these composite symbols in my drawing. 我在Canvas的OnRender方法中做了很多绘图,并希望在我的绘图中包含一些这些复合符号。 Is it possible get these into a format to draw them to the DrawingContext? 是否可以将它们转换为格式以将其绘制到DrawingContext? I am aware that there is a method called DrawingContext.DrawImage but this takes an ImageSource as input. 我知道有一个名为DrawingContext.DrawImage的方法,但这需要使用ImageSource作为输入。

Any ideas would be much appreciated. 任何想法将不胜感激。

Thanks, Alan 谢谢,艾伦

You can use drawimage if you make your canvas into an imagesource like: 如果将画布变成图像源,则可以使用drawimage:

VisualBrush vb = new VisualBrush(canvas);
GeometryDrawing gd = new GeometryDrawing(vb, new Pen(Brushes.Transparent, 0), new RectangleGeometry(new Rect(0, 0, width, height)));
img = new DrawingImage(gd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM