我试图创建一个显示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