3

I have a PictureBox as UserControl. I added this User Control on the main form. Now I have to press a button and create a line on the user control. On my project, every time I press this button, I want to send to user control parameters of two PointF(x and y) and draw a new line, in addition to the existent one. I have so far the Paint event when picturebox is loaded.

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
  Pen graphPen = new Pen(Color.Red, 2);
  PointF pt1D = new PointF();
  PointF pt2D = new PointF();
  pt1D.X = 0;
  pt1D.Y = 10;
  pt2D.X = 10;
  pt2D.Y = 10;

  e.Graphics.DrawLine(graphPen, pt1D, pt2D);
}
Chepene
  • 1,128
  • 1
  • 12
  • 18
Avrum
  • 269
  • 2
  • 8
  • 16

2 Answers2

3

Assuming that you want to draw the line on the click of the button, here's a modified version of your code:

List<PointF> points = new List<PointF>();
Pen graphPen = new Pen(Color.Red, 2);

private void btnDrawLines_Click(object sender, EventArgs e)
{
    Graphics g = picBox.CreateGraphics();
    PointF pt1D = new PointF();
    PointF pt2D = new PointF();
    pt1D.X = 0;
    pt1D.Y = 10;
    pt2D.X = 10;
    pt2D.Y = 10;    
    g.DrawLine(graphPen, pt1D, pt2D);
    points.Add(pt1D);
    points.Add(pt2D);
}

private void picBox_Paint(object sender, PaintEventArgs e)
{
    for (int i = 0; i < points.Count; i+=2)
        e.Graphics.DrawLine(graphPen, points[i], points[i + 1]);
}

Note that you can get a Graphics object through the PictureBox class's CreateGraphics() method which is the same as the e.Graphics object in the Paint event handler.

Abbas
  • 6,720
  • 4
  • 35
  • 49
  • `CreateGraphics` and `e.Graphics` aren't quite the same thing. Your version will not keep the drawing if the user minimizes and restores the form. – LarsTech Jan 20 '12 at 20:13
  • I will agree with you half-way. Everything drawn will go away after any event that forces the form or picture box to redraw, but CreateGraphics and e.Graphics return instances of the same Graphics class. Changes made in Paint handler persist because it is called after every redraw. What needs to be done is to preserve the lines drawn in the Click handler and redraw them in Paint. I will update my question to at least save the changes. – Abbas Jan 20 '12 at 20:21
  • Thanks, it looks ok. Notice that I need to press a button on main form, picture box is within a user control. I want to be able to send parameters(x,y, linecolor,etc) to this method and draw the line according to these parameters – Avrum Jan 20 '12 at 20:22
  • What you need seems to be a drawLineSegment(p1, p2, linecolor, g) method where p1 and p2 are points and g is a graphics object that you should be able to retrieve like this: Graphics g = userControl.pictureBox.CreateGraphics(). You then need to preserve p1 and p2 in the Points list. The Paint handler would remain as I have specified in my response. – Abbas Jan 20 '12 at 20:40
  • You are welcome. Take a look at the Line class idea as explained by @LarsTech too, it should make it easier to build whatever it is that you are building :). – Abbas Jan 21 '12 at 01:03
1

If you are adding lines to be drawn, the you probably want a little Line class:

public class Line {
  public Point Point1 { get; set; }
  public Point Point2 { get; set; }

  public Line(Point point1, Point point2) {
    this.Point1 = point1;
    this.Point2 = point2;
  }
}

And then you can just add these "lines" to a list:

private List<Line> _Lines = new List<Line>();

and add to them and tell the control to update it's drawing:

_Lines.Add(new Line(new Point(10, 10), new Point(42, 42)));
_Lines.Add(new Line(new Point(20, 40), new Point(20, 60)));
pictureBox1.Invalidate()

then in your drawing:

private void pictureBox1_Paint(object sender, PaintEventArgs e) {
  e.Graphics.Clear(Color.White);
  foreach (Line l in _Lines) {
    e.Graphics.DrawLine(Pens.Red, l.Point1, l.Point2);
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225