简体   繁体   中英

Drawing Line Arrays WPF

I'm trying to move my project from WinForms to WPF, but i couldn't succeed in this. I had a picture box, with a full of lines one after another and i could just use DrawLine(pen,point,point) in a loop. But when i tried to do this by using WPF, i couldn't menage.

This is the code snippet that i've used in 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);
}

And the result was : 在此处输入图片说明

I've tried to use Line array on WPF, but i gave an error on canvas.Children.Add line. Now i'm trying to use collection of points, but i can't arrange points as i want to. Here is what i've tried :

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

Use Stackpanel for your scenario. I have used a line array, which is similar to the thing which you have done in 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);
                }
            }
}

Sriman Reddy has answered my question already but i think i should post my own solution too :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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