2014-07-08 25 views
0

我试图创建一个显示picturebox参数是无效的,当我尝试使用位图

在线列车应用程序以实现这个我创建了一个worker thread以获得在线列车位置。所以我定义线程,你可以在这里看到:

private Thread workerThread = null; 
private delegate void UpdateListBoxDelegate(); 
private UpdateListBoxDelegate UpdateListBox = null; 

Form_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上网列车的位置,你可以在这里看到:

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 wi个大小x=Ay=b后,我创建另一个picturebox具有相同的大小和使用此代码把第二picturebox在第一picturebox

pictureBoxonlineTrain.Parent = pictureBoxMetroMap; 

但是,当我在这行中运行我的应用程序在DrawOnlineTrainParameter is not valid

map = new Bitmap(pictureBoxonlineTrain.Size.Width,pictureBoxonlineTrain.Size.Height);

enter image description here

堆栈跟踪:

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) 
+1

你有没有试过这种'新位图(pictureBoxonlineTrain.Width,pictureBoxonlineTrain.Height))' – Hassan

+0

@HassanNisar我用在我的代码 –

+1

尝试使用(从地图移除''该= using' ...' - 我认为当picturebox仍然在试图绘制它时,位图被放置了 – Blorgbeard

回答

3

我来处置,由于内存溢出异常的

不行,你必须处理你不使用图片任何更多。正确的代码是:

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; 
+0

它工作几分钟后,我得到了同样的错误在这里:位图地图=新的位图(pictureBoxonlineTrain.Size.Width,pictureBoxonlineTrain。Size.Height); –

+0

我给你的代码添加了一些细节,现在它工作正常 –

相关问题