2014-07-08 29 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

我給你的代碼添加了一些細節,現在它工作正常 –

相關問題