开发者

Drawing Basic Shapes in WPF InkCanvas

I am working on a paint like application in wpf.I want th开发者_StackOverflow中文版e users to be able to add some drawings over images or plain surfaces.Also i want to draw some basic shapes like line,ellipse or a rectangle.I am trying to work with an inkcanvas,where i can do freehand drawing,but i cant draw shapes like in paint.Can anyone guide me and provide some clues on how to do it.Please help me on this.Any inputs will be greatly appreciated.


There are two sorts of collections in an InkCanvas:

Strokes, which are composed of StylusPoints and defined by DrawingAttributes. That's what the Ink is, as drawn by a mouse or stylus.

The other is Children, which can contain FrameworkElements. Ellipse, for instance, is a Shape is a FrameworkElement.

Try playing around with yourCanvas.Children.Add(ellipse) and see how you go. There is certainly no reason to shy away from the InkCanvas just because you also want to use predefined shapes.

It's worth pointing out, though, that the InkCanvas's little brother, the InkPresenter, does NOT have a Children property. And Silverlight only has that one.


WPF provides a Shape class that includes prebuilt methods that you can draw shapes with. Don't use the inkcanvas and instead draw directly to a canvas.


Here http://ciintelligence.blogspot.com/2011/07/silverlight-drawing-tool-silver-draw.html you can find better control which improved SilverDraw control with extra features:

Freatures are: * You can draw basic shapes and also can draw using freehand pencil. * You can erase drawing. * You can undo and redo drawing. * Can save drawing as jpeg in server side.


Here is a simple implementation:

public void drawCircleAp(Double EHeight, Double EWidth, InkCanvas surface)
{
  Ellipse e1 = new Ellipse();
  e1.Width = EWidth;
  e1.Height = EHeight;
  var brush = new SolidColorBrush();
  brush.Color = Color.FromArgb(100, 0, 0, 0);
  e1.Stroke = brush;
  e1.StrokeThickness = 4;
  surface.Children.Add(e1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜