如何使用WPF [英] How can I draw in WPF

查看:72
本文介绍了如何使用WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Image中绘制一些图形元素(椭圆,矩形等).我写了下一个代码:

I need to draw few graphical elements in Image(ellipse, rectangle, etc.). I wrote next code:

protected GeometryDrawing PaintEllipse(Int32 Left, Int32 Top, Int32 Width, Int32 Height, Pen Pen)
       {
           GeometryDrawing returnedValue = null;

           EllipseGeometry ellipseGeometry = new EllipseGeometry(new Rect(Left, Top, Width, Height));

           returnedValue=new GeometryDrawing(null, Pen, ellipseGeometry);

           return returnedValue;
       }

       protected GeometryDrawing PaintRectangle(Int32 Left, Int32 Top, Int32 Width, Int32 Height, Pen Pen)
       {
           GeometryDrawing returnedValue = null;

           RectangleGeometry rectangleGeometry=new RectangleGeometry(new Rect(Left, Top, Width, Height));

           returnedValue=new GeometryDrawing(null, Pen, rectangleGeometry);

           return returnedValue;
       }


然后我写下一个代码:


Then I write next code:

image1.Source=new DrawingImage(PaintRectangle(0,0,190,90,new Pen(Brushes.Brown, 2)));


好吧!
但是我该如何画一些元素呢?有可能吗?


All right!
But how can I draw a few elements? Is it possible?

    void Drawing(String[] Elements, Image MyImage)
{
foreach(String currentElement in Elements)
{
switch(currentElement)
{
case("PaintEllipse"):
{
MyImage.Source+=PaintEllipse(0,0,40,70,new Pen(Brushes.Black, 3));
break;
}
case("PaintRectangle"):
{
MyImage.Source+=PaintRectangle(0,0,40,70,new Pen(Brushes.Black, 3));
break;
}
//etc.
}
}
}


例如.

推荐答案

我不确定我是否正确回答了您的问题.您想将一般图形/图像中的不同形状合并为一个吗?

如果是,请查看 DrawingGroup [ ^ ]类.

以下是您要实现的目标的示例:
I''m not sure if I got your question correctly. You want to merge different shapes, in general drawings/images into one?

If yes, have a look at the DrawingGroup[^] class.

Here an example for what you''d like to achieve:
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Image myImage = new Image();
        myImage.Source = this.DrawShapes( new[] { "PaintEllipse", "PaintRectangle" } );

        // drawingArea is a grid in the window
        drawingArea.Children.Add( myImage );
    }

    private ImageSource DrawShapes( IEnumerable<string> shapes )
    {
        DrawingGroup grp = new DrawingGroup();
        DrawingContext context = grp.Append();

        foreach ( string shape in shapes )
        {
            switch ( shape )
            {
                case "PaintEllipse":
                    {
                        context.DrawEllipse(
                            Brushes.Yellow, new Pen( Brushes.Green, 3 ), new Point( 0, 0 ), 40, 70 );
                        break;
                    }
                case "PaintRectangle":
                    {
                        context.DrawRectangle(
                            Brushes.Red, new Pen( Brushes.Black, 3 ), new Rect( 0, 0, 40, 70 ) );
                        break;
                    }
            }
        }

        context.Close();
        return new DrawingImage( grp );
    }
}



最好的问候



Best regards


这篇关于如何使用WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆