【问题标题】:call Paint event of picturebox from PageLoad in C# causes an error从 C# 中的 PageLoad 调用图片框的 Paint 事件导致错误
【发布时间】:2014-06-01 12:20:59
【问题描述】:

我创建了一个 PictureBox 来获取很多点并为我的应用程序绘制地图,所以这些点是从数据库中读取的,所以当我移动 picturebox 的滚动条时> 要查看我的地图的其他部分,再次调用 paint 事件,我的应用程序必须从数据库中读取点来绘制地图,但它不需要,因为我之前读过,所以从数据库中读取数据使我的应用程序变得很慢,所以我尝试在 Paint eventPage_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());



        }

这是我的图片框的 Pain 事件,在每次滚动移动后调用。我需要将此代码传输到 page_Load。我这样做了,但我的问题是如何我处理这些值:

(object sender, PaintEventArgs e)

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

我的页面加载:

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


        }

最好的问候

【问题讨论】:

  • 试图滥用 Paint 事件确实会将其变成 Pain 事件。您可以从 Graphics.FromImage() 获取 Graphics 对象。所以编写一个 DrawPicture(Graphics g) 方法并将代码移入其中。现在,您可以在 Load 事件处理程序中创建位图,调用此方法,然后分配 PictureBox.Image 属性。如果您需要调整地图,请再次执行相同的操作。

标签: c# winforms gdi+


【解决方案1】:

您不应在 OnPaint 中读取数据库,也不应在 OnLoad 中在屏幕上绘图。

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

【讨论】:

  • 是的,你是对的,但我之前做过,而且它有效,但我需要在 page_load 中绘制我的地图,因为它使我的应用程序运行得如此之快,我不需要每次都画我的地图
  • 然后将其绘制为位图。 OnPaint 只能显示(部分)该位图。
猜你喜欢
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多