Add point to canvas

I am coding in Microsoft Visual Studio 2010 Express for Windows Phone. I need to add a point to the 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); } 

The studio says:

Error 2 Argument 1: Cannot convert from "System.Windows.Point" to "System.Windows.UIElement"

My question is: how to add a point to a Canvas ?

+4
source share
5 answers

From your code snippet, I assume that you are trying to draw a curve. To do this, you can look at GraphicsPath . Instead of drawing individual points, you can use points in the form of coordinates that you connect along lines. Then in your code, you can create a GraphicsPath using the AddLine method.

Then it could be drawn on a bitmap, for example.

EDIT

Example (not verified):

 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 is to use the WPF Path class, which will work in roughly the same way, but is a real user interface element that you can add to child Canvas elements.

EDIT

People have indicated that the code above is Windows Forms code. Well, here is 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
source

The Point used is not a UIElement , but a structure, 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

+1
source

If this is β€œonly one point you want to add, you can add a tiny rectangle or ellipse to the canvas.

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

+1
source

According to the error, the children of the Canvas control must be derived from the System.Windows.UIElement : System.Windows.Point class. To achieve what you are doing, it is best to use geometry in WPF. See here for an article on how to do this.

0
source

Try adding an 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); 
0
source

Source: https://habr.com/ru/post/1391650/


All Articles