我正在开发一个涂料应用程序。我使用Canvas控件作为白板。
我想在WPF画布中使用InkCanvas类功能,意味着我想在Canvas上绘制一个自由形状。我怎样才能做到这一点 ?我也想保存为* .JPG
第二个问题是像在MS Paint中那样绘制字体?我可以在其中集成FontDialogBox吗?如果是,怎么样?
感谢你!!!
答案 0 :(得分:6)
这是一个在画布上绘制线条以及如何添加textBlock对象的简单示例。
示例:
<StackPanel>
<Canvas MouseLeftButtonDown="myCanvas_MouseLeftButtonDown" MouseMove="myCanvas_MouseMove"
x:Name="inkCanvas" Width="400" Height="200"> </Canvas>
<Button Height="20" Width="30" x:Name="AddTextBtn" Click="AddTextBtn_Click">AddText</Button>
</StackPanel>
代码背后:
private Point _startPoint;
private Polyline _line;
private void myCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(myCanvas);
_line = new Polyline();
_line.Stroke = new SolidColorBrush(Colors.Black);
_line.StrokeThickness = 2.0;
myCanvas.Children.Add(_line);
}
private void myCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point currentPoint = e.GetPosition(myCanvas);
if (_startPoint != currentPoint)
{
_line.Points.Add(currentPoint);
}
}
}
private void AddTextBtn_Click(object sender, RoutedEventArgs e)
{
// Show Font Dialog Box
TextBlock tb = new TextBlock();
tb.FontFamily = new FontFamily("Arial");
tb.FontSize = 14;
tb.Text = "SampleText";
// Set position on canvas:
Canvas.SetTop(tb,100);
Canvas.SetLeft(tb, 50);
myCanvas.Children.Add(tb);
}
但是没有纯WPF字体选择器。您既可以使用WinForms中的那个,也可以查看有很多纯Xaml实现。例如。 link
另存为jpg:使用RenderTargetBitmap或CroppedBitmap; See link on SO