2010年11月15日

如何在C#的Picturebox上畫線畫圓

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

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

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

沒有留言:

張貼留言