从C#中的Page Load调用picturebox的Paint事件会导致错误

时间:2014-06-01 12:20:59

标签: c# winforms gdi+

我创建了一个 PictureBox ,它可以获得很多点并为我的应用程序绘制一个地图,所以这些点是从数据库中读取的,所以当我移动我的 picturebox的滚动要查看我的地图的其他部分,再次调用绘制事件,我的应用程序必须从数据库读取点来绘制地图,但它不需要,因为我之前读过,所以从数据库中读取数据会使我的应用程序变得如此之慢,所以我尝试在绘制事件中找到我的代码到 Page_load ,因为这种转移使我的代码只运行一度 , 那么让我解释一下我的代码:

private void pictureBoxMetroMap_Paint(object sender, PaintEventArgs e)
        {
            Pen pLine = new Pen(Color.White, 2);
            SolidBrush RedBrush = new SolidBrush(ColorTranslator.FromHtml("#cc0000"));
            SolidBrush blueBrush = new SolidBrush(ColorTranslator.FromHtml("#0066ff"));
            SolidBrush greenBrush = new SolidBrush(ColorTranslator.FromHtml("#3db300"));
            SolidBrush whiteBrush = new SolidBrush(ColorTranslator.FromHtml("#fff"));



            SensorRepository objSensorRepository = new SensorRepository();
            List<Sensor> lstSensorLeft = objSensorRepository.GetSensorsLine(1, "Left");

            List<Point> lstPointLeft = new List<Point>();
            foreach (var t in lstSensorLeft)
            {
                Point objPoint = new Point(t.XLocation, t.YLocation);
                lstPointLeft.Add(objPoint);
                Rectangle rectSens = new Rectangle(t.XLocation, t.YLocation, 3, 3);
                e.Graphics.FillRectangle(whiteBrush, rectSens);
                if (t.StationId != null)
                {
                    Rectangle rectEhsansq = new Rectangle(t.XLocation-6, t.YLocation-6, 12, 12);
                    e.Graphics.FillRectangle(blueBrush, rectEhsansq);

                    Rectangle rectTrainState = new Rectangle(t.XLocation -3, t.YLocation-3, 7, 7);
                    e.Graphics.FillRectangle(RedBrush, rectTrainState);
                }
            }
            StationRepository ObjStationRepository = new StationRepository();

            List<Sensor> lstSensorRight = objSensorRepository.GetSensorsLine(1, "Right");
            List<Point> lstPointRight = new List<Point>();

            foreach (var t in lstSensorRight)
            {
                Point objPoint = new Point(t.XLocation+30, t.YLocation+30);
                lstPointRight.Add(objPoint);
                Rectangle rectSens = new Rectangle(t.XLocation+30, t.YLocation+30, 3, 3);
                e.Graphics.FillRectangle(whiteBrush, rectSens);
                if (t.StationId!= null)
                {
                    Rectangle rectPosition= new Rectangle(t.XLocation + 24, t.YLocation + 24, 12, 12);
                    e.Graphics.FillRectangle(blueBrush, rectPosition);

                    Rectangle rectTrainState = new Rectangle(t.XLocation + 27, t.YLocation + 27, 7, 7);
                    e.Graphics.FillRectangle(RedBrush, rectTrainState);

                }
            }


            e.Graphics.DrawLines(pLine, lstPointLeft.ToArray());
            e.Graphics.DrawLines(pLine, lstPointRight.ToArray());



        }

这是我的图片框的痛苦事件,在每次滚动动作后都会被调用。我需要将此代码转移到 page_Load 。我这样做但是我的问题是怎样才能我处理这些价值观:

(object sender, PaintEventArgs e)

因为page_load没有任何 PaintEventArgs 参数。我的意思是我如何将此代码转移到 page_load

我的page_load:

 private void frmMain_Load(object sender, EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.None;


        }

祝你好运

1 个答案:

答案 0 :(得分:1)

你不应该从OnPaint的数据库中读取,你不应该在OnLoad的屏幕上绘图。

所以划分工作:在OnLoad中,获取并存储数据。在OnPaint中,绘制缓存的数据。