Add Point to Canvas

↘锁芯ラ 提交于 2019-12-14 01:12:50

问题


I'm coding in Microsoft Visual Studio 2010 Express for Windows Phone. I need to add a point onto a Canvas, but I can't...

for (float x = x1; x < x2; x += dx)
{
    Point poin = new Point();
    poin.X = x;
    poin.Y = Math.Sin(x);
    canvas1.Children.Add(poin);
}

Studio says:

Error 2 Argument 1: cannot convert from 'System.Windows.Point' to 'System.Windows.UIElement'

My question is: how do I add a point onto a Canvas?


回答1:


From your code snippet I assume you're trying to draw a curve. To do this, you can look into GraphicsPath. Instead of drawing individual points, you can use the points as coordinates, which you connect through lines. Then, in your code, you can create a GraphicsPath using the AddLine method.

This could then be drawn onto a bitmap, for example.

EDIT

Sample (not tested):

GraphicsPath p = new GraphicsPath();

for (float x = x1; x < x2; x += dx)
{
    Point point = new Point();
    point.X = x;
    point.Y = Math.Sin(x);

    Point point2 = new Point();
    point2.X = x+dx;
    point2.Y = Math.Sin(x+dx);

    p.AddLine(point, point2);
}

graphics.DrawPath(p);

Another way would be to use the WPF Path class, which would work about the same, but is a real UI element which you can add to the children of a Canvas.

EDIT

People have pointed out that the above code is Windows Forms code. Well, here's what you can do in WPF:

myPolygon = new Polygon();
myPolygon.Stroke = System.Windows.Media.Brushes.Black;
myPolygon.Fill = System.Windows.Media.Brushes.LightSeaGreen;
myPolygon.StrokeThickness = 2;
myPolygon.HorizontalAlignment = HorizontalAlignment.Left;
myPolygon.VerticalAlignment = VerticalAlignment.Center;

PointCollection points = new PointCollection();
for (float x = x1; x < x2; x += dx)
{
    Point p = new Point(x, Math.Sin(x));
    points.Add(p);
}

myPolygon.Points = points;
canvas1.Children.Add(myPolygon);



回答2:


If it is 'just a single point you want to add, you can add a tiny rectangle or ellipse to the canvas.

If you want to set a lot of points or a couple points many times, I suggest you create an array of pixel data (colors) and write those to a WriteableBitmap




回答3:


The Point you used is not a UIElement but a struct, please use Line instead.

Line lne = new Line();
lne.X1 = 10;
lne.X2 = 11;
lne.Y1 = 10;
lne.Y2 = 10;
canvas1.Children.Add(lne);

You get the idea...

Edit
changed: lne.X2 = 10 to lne.X2 = 11




回答4:


As per the error, the children of the Canvas control must be derivatives of the System.Windows.UIElement class: System.Windows.Point is not. To achieve what you are doing, you would be best looking into using the geometry within WPF. See here for an article on how to do so.




回答5:


Try adding a ellipse

Ellipse myEllipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
myEllipse.Fill = mySolidColorBrush;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.White;
myEllipse.Width = 200;
myEllipse.Height = 100;
Canvas.SetTop(myEllipse,50);
Canvas.SetLeft(myEllipse,80);
myCanvas.Children.Add(myEllipse);


来源:https://stackoverflow.com/questions/8911713/add-point-to-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!