绘制线阵列WPF

时间:2015-03-31 10:09:08

标签: c# wpf line draw

我正在尝试将我的项目从WinForms转移到WPF,但我无法在此成功。我有一个图片框,一个接一个的线条,我可以在循环中使用DrawLine(笔,点,点)。但是当我试图通过使用WPF来做到这一点时,我无法管理。

这是我在WinForms中使用的代码片段:

for (x = 0; x < X_COORD_END - X_COORD_START; x += 1)
{
                System.Drawing.Point point1 = new System.Drawing.Point(x, 30);
                System.Drawing.Point point2 = new System.Drawing.Point(x, 60);
                e.Graphics.DrawLine(myPen, point1, point2);
}

结果是:enter image description here

我试过在WPF上使用Line数组,但我在canvas.Children.Add行上给出了一个错误。现在我正在尝试使用积分,但我不能像我想的那样安排积分。这是我尝试过的:

private void DrawLine(Point[] points)
    {
        Polyline line = new Polyline();
        PointCollection collection = new PointCollection();
        foreach (Point p in points)
        {
            collection.Add(p);
        }
        line.Points = collection;
        line.Stroke = new SolidColorBrush(Colors.Black);
        line.StrokeThickness = 20;
        scanCanvas.Children.Add(line);
    }


    for (int counter = 0; counter < 1000; counter++ )
        {
            points[counter] = new Point(counter, 30);
        }

        DrawLine(points);

2 个答案:

答案 0 :(得分:1)

将Stackpanel用于您的方案。我使用了一个行数组,这类似于你在winforms中所做的事情。

<强> MainWindow.xaml

<Window x:Class="SO.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="stackGraphicsArea" Orientation="Horizontal"/>
</Window>

<强> MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
     public MainWindow()
            {
                InitializeComponent();
                Line[] lines = new Line[1000];
                for(int i=0; i<1000;i++)
                {
                    lines[i] = new Line();
                    lines[i].Stroke = new SolidColorBrush(Colors.Red);
                    lines[i].StrokeThickness = 5;
                    lines[i].X1 = 0;
                    lines[i].X2 = 0;
                    lines[i].Y1 = 30;
                    lines[i].Y2 = 20;
                }
                DrawLines(lines);
            }

    private void DrawLines(Line[] _lines)
            {
                foreach (Line _line in _lines)
                {
                    stackGraphicsArea.Children.Add(_line);
                }
            }
}

答案 1 :(得分:0)

Sriman Reddy已经回答了我的问题,但我想我也应该发布自己的解决方案:

private void DrawLine(int x_coord_start, int y_coord_start, int x_coord_end, int thickness, Color color)
    {
        Random rand = new Random();
        for (int x = 0; x < x_coord_end - x_coord_start; x++)
        {
            double newTop = rand.Next();

            Line top = new Line();
            top.Stroke = new SolidColorBrush(color);
            top.StrokeThickness = x + 1;

            top.X1 = x_coord_start + x;
            top.Y1 = y_coord_start;
            top.X2 = x_coord_start + x;
            top.Y2 = y_coord_start + thickness;

            Canvas.SetTop(top, 0);
            Canvas.SetLeft(top, 0 /*x * top.Width*/);
            scanCanvas.Children.Add(top);
        }
    }