在 PictureBox 上从父级画一条线

发布于 2024-12-27 21:33:23 字数 519 浏览 0 评论 0原文

我有一个 PictureBox 作为 UserControl。我在主窗体上添加了这个用户控件。现在我必须按下一个按钮并在用户控件上创建一条线。在我的项目中,每次按下此按钮时,除了现有的一条线之外,我还想向用户发送两个 PointF(x 和 y)的控制参数并绘制一条新线。到目前为止,当加载图片框时,我有 Paint 事件。

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2025-01-03 21:33:23

假设您想在单击按钮时绘制线条,这是代码的修改版本:

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]);
}

请注意,您可以通过 PictureBox 类的 CreateGraphics() 方法,与 Paint 事件处理程序中的 e.Graphics 对象相同。

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.

初懵 2025-01-03 21:33:23

如果您要添加要绘制的线条,您可能需要一个 Line 类:

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;
  }
}

然后您可以将这些“线条”添加到列表中:

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

然后添加到它们并告诉控件更新它的绘图:

_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()

然后在您的绘图中:

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);
  }
}

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文