我試圖創建一個顯示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=A
和y=b
後,我創建另一個picturebox
具有相同的大小和使用此代碼把第二picturebox
在第一picturebox
:
pictureBoxonlineTrain.Parent = pictureBoxMetroMap;
但是,當我在這行中運行我的應用程序在DrawOnlineTrain
了Parameter is not valid
。
map = new Bitmap(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)
你有沒有試過這種'新位圖(pictureBoxonlineTrain.Width,pictureBoxonlineTrain.Height))' – Hassan
@HassanNisar我用在我的代碼 –
嘗試使用(從地圖移除''該= using' ...' - 我認爲當picturebox仍然在試圖繪製它時,位圖被放置了 – Blorgbeard