1

I am looking to do some simple drawing with in a WPF application, I was wondering if this is a simple task or if I should look into some 3rd party libraries.

Quite simply I just want to draw on an image control some points and lines between those points. Is this something easy to do?

thanks!

Herrozerro
  • 1,601
  • 1
  • 22
  • 37
  • Yes, it can be done with a basic knowledge of shapes provide in System.Windows.Shapes namespace .http://msdn.microsoft.com/en-us/library/system.windows.shapes.aspx – Nitesh Jul 19 '13 at 13:56
  • How would one draw that to an image control? – Herrozerro Jul 19 '13 at 14:10
  • 1
    You can set your image as Background of the container control (Canvas) using ImageBrush and Draw them simply on your Canvas. – Nitesh Jul 19 '13 at 14:12
  • You may as well overlay a Canvas (which is transparent by default) over an Image control. Put both in a Grid. – Clemens Jul 19 '13 at 14:29

2 Answers2

2

for a sample to use ..you can draw a line using basics only...

how to set canvas backgroud as image..

  ImageBrush ib = new ImageBrush();
ib.ImageSource = new BitmapImage(new Uri(@"sampleImages\berries.jpg", UriKind.Relative));
mycanvas.Background = ib;

and you can now draw on your canvas a line like this..

line = new Line();
line.Stroke = Brushes.LightSteelBlue;
line.X1 = 1;
line.X2 = 50;
line.Y1 = 1;
line.Y2 = 50;
line.StrokeThickness = 2;
myCanvas.Children.Add(line);

hope it will help you to start..

loop
  • 9,002
  • 10
  • 40
  • 76
  • Quick question, I modified it to be:Image ib = new ImageBrush(); ib.ImageSource = new BitmapImage(); canvas1.Background = ib; But I seem t oget an issue: "BitmapImage has not been initialized. Call the BeginInit method, set the appropriate properties, and then call the EndInit method." – Herrozerro Jul 19 '13 at 19:46
  • Nevermind, I actually dont need the image, just draw the shapes into the canvas. – Herrozerro Jul 19 '13 at 20:03
0

You might want to take a look at ZedGraph:

ZedGraph is a class library, user control, and web control for .net, written in C#, for drawing 2D Line, Bar, and Pie Charts. It features full, detailed customization capabilities, but most options have defaults for ease of use.

I think that you would be re-inventing the wheel if you do it yourself. Also, requirements can change with time, so you might need to add some more items to your graph eventually.

npinti
  • 51,780
  • 5
  • 72
  • 96