如何在XAML画布中绘制随机/不规则线?

时间:2016-03-16 07:01:40

标签: c# wpf xaml win-universal-app

我在XAML中有一个画布,我想用C#绘制随机不规则(手绘)线。

这就是我想要的:http://jsfiddle.net/GfGVE/9/ HTML Canvas但在XAML C#

我想要水平绘制线条。

Xaml:

<Canvas Grid.Column="1" x:Name="ItemCanvas"></Canvas>

C#:

public ItemControl()
{
    this.InitializeComponent();
    canvas = this.ItemCanvas;    
}

2 个答案:

答案 0 :(得分:1)

您可以将InkCanvas用于此目的。它定义了一个接收和显示墨迹笔划的区域:

<Grid>
    <InkCanvas Name="InkCanvas1"></InkCanvas>
</Grid>

结果:

enter image description here

答案 1 :(得分:1)

您需要为事件for (x = 0; x < width; x++){ for (y = 0; y < height; y++){ bmp.setPixel(x, y, bitMatrix.get(x,y) ? Color.BLACK : Color.WHITE); } } MouseMoveMouseDownMouseEnterMouseUp添加方法。此外,您需要两个变量用于当前位置和最后一个位置。如果您还想撤消,则需要为此添加MouseLeave。这些是所需的方法:

Stack<int>

要从画布中移除最后一行,您可以调用此private void Canvas_MouseDown(Object sender, MouseButtonEventArgs e) { // push start of line onto undo stack this.undo.Push(this.paintCanvas.Children.Count); this.last = e.GetPosition(this.paintCanvas); } private void Canvas_MouseMove(Object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { if (this.last.X != -1) { Point current = e.GetPosition(this.paintCanvas); Line l = new Line(); l.X1 = this.last.X; l.Y1 = this.last.Y; l.X2 = current.X; l.Y2 = current.Y; l.Stroke = this.brush; l.StrokeThickness = this.thickness; this.paintCanvas.Children.Add(l); this.last = current; } } } private void Canvas_MouseUp(Object sender, MouseButtonEventArgs e) { // push end of line onto undo stack this.undo.Push(this.paintCanvas.Children.Count); } private void Canvas_MouseLeave(Object sender, MouseEventArgs e) { this.last = new Point(-1, -1); } private void Canvas_MouseEnter(Object sender, MouseEventArgs e) { this.last = e.GetPosition(this.paintCanvas); } - 方法:

Undo