How can I use the Pen tool to draw a simple black line on a PictureBox's Image object?

StackOverflow https://stackoverflow.com/questions/4696202

  •  11-10-2019
  •  | 
  •  

문제

I'm trying to draw a line that goes from middle top to bottom.

I know I have to use the Pen class to accomplish this.

private void RepaintPreview()
{
    Pen blackPen = new Pen(Brushes.Black);
    blackPen.Width = 1.0f;
    blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
    ptbTablePreview.Image.draw?
}

Basically, how can I draw this line on the image? Thank you.

도움이 되었습니까?

해결책

I tried the following, and it works with me:

1- I set the image of the picturebox in design time

2- I handled the Paint event of the picturebox, and added the following code:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Pen p = new Pen(Color.Black, 2))
    {
        e.Graphics.DrawLine(p, new Point(pictureBox1.Width / 2, 0), new Point(pictureBox1.Width / 2, pictureBox1.Height));
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top