4

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?

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
Anatoly
  • 141
  • 1
  • 2
  • 8
  • 1
    The `Point` class does not represent a visual point, but is just a set of coordinates that you could use to define **where** your point should be displayed. – Anders Marzi Tornblad Jan 18 '12 at 14:29

5 Answers5

4

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);
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • Microsoft Visual Studio 2010 Express have no System.Drawing.dll – Anatoly Jan 18 '12 at 14:41
  • 1
    @Anatoly: this is not a matter of the Visual Studio version you're using. System.Drawing is part of the .NET Framework. VS 2010 takes a while to asynchronously load the assemblies you can add as reference. Wait some more and sort the list by name. – Thorsten Dittmar Jan 18 '12 at 14:58
  • @Erno: Yes, but doesn't really matter, does it? You can still use `GraphicsPath` and draw it to a bitmap, which then you show on a canvas, right? :-) – Thorsten Dittmar Jan 18 '12 at 15:01
  • @ThorstenDittmar - I'd go for a WPF solution because I expect performace to be better. The Graphics class uses GDI and it seems such a waste to get into the whole mess of Disposing GDI objects in a WPF application. – Emond Jan 18 '12 at 15:08
1

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

Sonosar
  • 526
  • 2
  • 12
  • But how i can put it in for? Studio say: Element is already the child of another element. – Anatoly Jan 18 '12 at 14:39
  • it is possible only if you will try to add the same Line twice, please add Line lne = new Line(); in for block not before that. – Sonosar Jan 18 '12 at 14:41
  • please remove any other code and use the code you posted, add it to Loaded event of your MainPage. also I was wrong assuming 0 size point could work, please change lne.X2 = x ----> lne.X2 = x+1 – Sonosar Jan 18 '12 at 15:04
1

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

Emond
  • 50,210
  • 11
  • 84
  • 115
0

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.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
0

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);
jcvegan
  • 3,111
  • 9
  • 43
  • 66