添加点到画布 [英] Add Point to Canvas

查看:187
本文介绍了添加点到画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码在Microsoft Visual Studio 2010 Express的Windows手机。我需要把点添加到画布,但我不能......

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

演播室说:

错误2参数1:无法从System.Windows.Point'到'System.Windows.UIElement

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?

推荐答案

从您的代码段我假设你想绘制一条曲线。要做到这一点,你可以看看的GraphicsPath 。相反,绘制单独的点,你可以使用点作为坐标,你通过线连接。然后,在你的代码,你可以创建一个的GraphicsPath 使用 AddLine 方法。

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.

修改

样品(未测试):

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



另一种方法是使用WPF 路径类,它会工作差不多的,但你可以添加到画布的孩子们一个真正的UI元素。

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.

修改

人指出,上面的代码是Windows窗体的代码。那么,这里就是你可以在WPF做的:

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

这篇关于添加点到画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆