我对C#并不太熟悉,只是知道一些基础,原来我一直认为PictureBox.CreateGraphics()和e.Graphics;都是获得了Graphics对象没什么区别,但是 我前一阵子遇到了这样一个问题:
向下面这样写就一切正常,如果将g=e.Graphics;改成pictureBox.CreateGraphics()就不好使了(pictureBox是一个已定义的控件)
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {            Graphics g;
            g = e.Graphics;
            
            \\g=pictureBox.CreateGraphics();            Pen myPen = Pens.Blue;
            for (int i = 0; i < pictureBox.ClientRectangle.Width; i++)
            {
                g.DrawLine(myPen, new Point(i, 0), new Point(i, pictureBox.ClientRectangle.Bottom));
                i += 10;
            }            for (int j = 0; j < pictureBox.ClientRectangle.Height; j++)
            {
                g.DrawLine(myPen, new Point(0, j), new Point(pictureBox.ClientRectangle.Right, j));
                j += 10;
            }
            g.Dispose();        }

解决方案 »

  1.   

    e.Graphics 获得的是触发这个事件的控件的图形句柄,来自参数
    而 PictureBox.CreateGraphics() 则不与当前控件关联
      

  2.   


    可是,绘图是在PictureBox控件上的呀,触发该事件的控件,不就是PictureBox么???
      

  3.   

    private void pictureBox_Paint(object sender, PaintEventArgs e)个人认为e应该绘制这个pictureBox的图形句柄
    PictureBox.CreateGraphics()是将要在PictureBox上绘制图形所要用到的句柄
      

  4.   

    如果打算自己绘图,你可以考虑用Panel。
    反之。你可以
    Graphics g = Graphics.FromImage(PictureBox1.Image);
      

  5.   

    画图应该尽量在picturebox上画,这样不容易乱。思路清晰
      

  6.   


    GDI+。。
    建议在panel上绘制。