2020年9月22日 星期二

在C#的Picturebox上畫線畫圓

 在C#的Picturebox上畫線畫圓, 以下是相關介紹:


可先參考 Graphics 成員
實作(注意*那行Graphics objGraphic = e.Graphics;):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
            int picBoxWidth = pictureBox1.Size.Width;
            int picBoxHeight = pictureBox1.Size.Height;
            int halfWidth = pictureBox1.Size.Width / 2;
            int halfHeight = pictureBox1.Size.Height / 2;
            Graphics objGraphic = e.Graphics; //**請注意這一行**
            Pen pen = new Pen(Color.Black);
            int b = 2; int m = 2; for (int x = 0; x < 426; x++)
            {
                int y = m * x + b; x = (y - b) / m;
                objGraphic.DrawLine(pen, x, y, -x, -y);
                System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
                objGraphic.Restore(graph);
            }
            objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
            objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);
            objGraphic.DrawLine(pen, 20, 20, 300, 300);
}


資料來源 http://trufflepenne.blogspot.com/2010/11/cpicturebox.html

沒有留言: