Anatoly
Anatoly

Reputation: 141

Add Point to Canvas

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?

Upvotes: 4

Views: 23914

Answers (5)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

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

Upvotes: 4

Sonosar
Sonosar

Reputation: 526

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

Upvotes: 1

jcvegan
jcvegan

Reputation: 3170

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

Upvotes: 0

Emond
Emond

Reputation: 50672

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

Upvotes: 1

Samuel Slade
Samuel Slade

Reputation: 8613

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.

Upvotes: 0

Related Questions