C# 尝试使用位图时,参数无效

C# 尝试使用位图时,参数无效,c#,multithreading,image,bitmap,picturebox,C#,Multithreading,Image,Bitmap,Picturebox,我正在尝试创建一个应用程序,该应用程序在picturebox中显示在线列车 因此,为了实现这一点,我创建了一个工作线程,以获取在线列车位置。因此,我定义了线程,如您所见: private Thread workerThread = null; private delegate void UpdateListBoxDelegate(); private UpdateListBoxDelegate UpdateListBox = null; public void GetOnlineTrain()

我正在尝试创建一个应用程序,该应用程序在picturebox中显示在线列车

因此,为了实现这一点,我创建了一个
工作线程
,以获取在线列车位置。因此,我定义了线程,如您所见:

private Thread workerThread = null;
private delegate void UpdateListBoxDelegate();
private UpdateListBoxDelegate UpdateListBox = null;
public void GetOnlineTrain()
{
    try
    {
        while (true)
        {
            TimeTableRepository objTimeTableREpository = new TimeTableRepository();
            OnlineTrainList = objTimeTableREpository.GetAll().ToList();
            objTimeTableREpository = null;
            Invoke(UpdateListBox);
        }
    }
    catch(Exception a)
    {
    }

}
Form\u load
中,我称之为:

            UpdateListBox = new UpdateListBoxDelegate(this.UpdateStatus);
            // Initialise and start worker thread
            workerThread = new Thread(new ThreadStart(this.GetOnlineTrain));
            workerThread.Start();
委托处理的我的方法是:

private void UpdateStatus()
{
    foreach (TimeTable onlineTrain in OnlineTrainList.ToList())
    {
        if (lstSensorLeft.Count != 0 || lstSensorRight.Count != 0)
        {
            pictureBoxonlineTrain.Image = null;

            DrawOnlineTrain();
        }
        else
        {
            pictureBoxonlineTrain.Image = null;
        }
    }

    this.Invalidate();
}
GetOnlineTrain
获取在线列车的位置,如下所示:

private Thread workerThread = null;
private delegate void UpdateListBoxDelegate();
private UpdateListBoxDelegate UpdateListBox = null;
public void GetOnlineTrain()
{
    try
    {
        while (true)
        {
            TimeTableRepository objTimeTableREpository = new TimeTableRepository();
            OnlineTrainList = objTimeTableREpository.GetAll().ToList();
            objTimeTableREpository = null;
            Invoke(UpdateListBox);
        }
    }
    catch(Exception a)
    {
    }

}
最后一个函数在
picturebox
上绘制在线列车:

       public void DrawOnlineTrain()
    {
        Bitmap map;
        using (map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height))
        {
            if (OnlineTrainList.Count > 0)
            {
                using (Graphics graph = Graphics.FromImage(map))
                {
                    foreach (TimeTable t in OnlineTrainList.ToList())
                    {
                        // graph.Dispose();
                        Rectangle rectTrainState = new Rectangle(t.XTrainLocation.Value - 3,
                                                                 t.YTrainLocation.Value - 3,
                                                                 15, 15);
                        graph.FillRectangle(RedBrush, rectTrainState);
                        graph.DrawString(t.TrainId.ToString(), pictureBoxonlineTrain.Font, Brushes.White, t.XTrainLocation.Value - 3, t.YTrainLocation.Value - 3);
                        pictureBoxonlineTrain.Image = map;

                    }
                }
            }
        }


    }
为了第一次绘制在线列车,我在
picturebox
上绘制了大小为
x=A
y=b
的列车地图(线路、车站等),然后我创建了另一个大小相同的
picturebox
,并使用此代码将第二个
picturebox
放在第一个
picturebox
上:

    pictureBoxonlineTrain.Parent = pictureBoxMetroMap;
但当我在这行中运行应用程序时,我在
drawinlinetrain
中得到的
参数无效

map=新位图(pictureBoxonlineTrain.Size.Width,pictureBoxonlineTrain.Size.Height)

堆栈跟踪:

   at System.Drawing.Image.get_Width()
   at System.Drawing.Image.get_Size()
   at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
由于内存不足,我必须处理它

不,您必须处理不再使用的图像。正确的代码是:

   Bitmap map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height))
   using (Graphics graph = Graphics.FromImage(map)) {
       graph.Clear(this.BackColor);
       foreach (TimeTable t in OnlineTrainList.ToList()) {
           Rectangle rectTrainState = new Rectangle(...);
           graph.FillRectangle(RedBrush, rectTrainState);
           graph.DrawString(...);
       }
   }
   if (pictureBoxonlineTrain.Image != null) pictureBoxonlineTrain.Image.Dispose();
   pictureBoxonlineTrain.Image = map;
由于内存不足,我必须处理它

不,您必须处理不再使用的图像。正确的代码是:

   Bitmap map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height))
   using (Graphics graph = Graphics.FromImage(map)) {
       graph.Clear(this.BackColor);
       foreach (TimeTable t in OnlineTrainList.ToList()) {
           Rectangle rectTrainState = new Rectangle(...);
           graph.FillRectangle(RedBrush, rectTrainState);
           graph.DrawString(...);
       }
   }
   if (pictureBoxonlineTrain.Image != null) pictureBoxonlineTrain.Image.Dispose();
   pictureBoxonlineTrain.Image = map;

您是否尝试过此
新位图(pictureBoxonlineTrain.Width,pictureBoxonlineTrain.Height))
@Hassansar我在代码中使用了它尝试从
使用中删除
(map=…
-我认为位图正在被处理,而picturebox仍在尝试绘制它。您处理了它,小心盲目地应用using语句。@Blorgbeard我在这个问题中删除了using语句,但内存不足异常。请看一看:您尝试过这个
新位图吗(pictureBoxonlineTrain.Width,pictureBoxonlineTrain.Height))
@hassansar我在代码中使用了它尝试从
使用中删除
(map=…
-我认为位图正在被处理,而picturebox仍在尝试绘制它。您处理了它,小心盲目地应用using语句。@Blorgbeard我在这个问题中删除了using语句,但内存不足异常。请您看一下:几分钟后它会工作几分钟es我在这里遇到了相同的错误:位图映射=新位图(pictureBoxonlineTrain.Size.Width,pictureBoxonlineTrain.Size.Height);我在代码中添加了一些细节,现在它可以正常工作几分钟。几分钟后,我在这里遇到了相同的错误:位图映射=新位图(pictureBoxonlineTrain.Size.Width,pictureBoxonlineTrain.Size.Height);我在代码中添加了一些细节,现在可以正常工作了