[英]Parameter is not valid when i try to use bitmap
I am trying to create an application that shows the online trains in picturebox
我正在尝试创建一个在图片picturebox
显示在线火车的应用程序
So to implement this i create a worker thread
to get the online train position .so i define the thread as you can see here : 因此要实现此目的,我创建了一个worker thread
来获取在线火车位置。因此,我定义了线程,如下所示:
private Thread workerThread = null;
private delegate void UpdateListBoxDelegate();
private UpdateListBoxDelegate UpdateListBox = null;
In Form_load
i call these: 在Form_load
我称这些为:
UpdateListBox = new UpdateListBoxDelegate(this.UpdateStatus);
// Initialise and start worker thread
workerThread = new Thread(new ThreadStart(this.GetOnlineTrain));
workerThread.Start();
My method that delegate handles that is : 我委托处理的方法是:
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();
}
The GetOnlineTrain
get the position of online train as you can see here : GetOnlineTrain
获取在线火车的位置,您可以在这里看到:
public void GetOnlineTrain()
{
try
{
while (true)
{
TimeTableRepository objTimeTableREpository = new TimeTableRepository();
OnlineTrainList = objTimeTableREpository.GetAll().ToList();
objTimeTableREpository = null;
Invoke(UpdateListBox);
}
}
catch(Exception a)
{
}
}
And the final function draws the online trains on the picturebox
: 最后一个功能将在线火车绘制在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;
}
}
}
}
}
To draw online train first time i draw the map of trains (lines ,stations ,...) on picturebox
with size x=A
and y=b
after that i create another picturebox
with the same size and put the second picturebox
on first picturebox
using this code: 要绘制在线列车第一次上绘制列车的地图(线,站,...) picturebox
与大小x=A
和y=b
之后,我创建另一个picturebox
具有相同大小,并把该第二picturebox
上第一picturebox
使用此代码:
pictureBoxonlineTrain.Parent = pictureBoxMetroMap;
But when i run my application in this line i got Parameter is not valid
in DrawOnlineTrain
. 但是当我在这一行中运行我的应用程序时,我在DrawOnlineTrain
中DrawOnlineTrain
了Parameter is not valid
。
map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height); map = new Bitmap(pictureBoxonlineTrain.Size.Width,pictureBoxonlineTrain.Size.Height);
stack trace: 堆栈跟踪:
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)
i have to dispose that because of out of memory exception 由于内存不足异常,我不得不进行处理
No, you have to dispose images you don't use any more. 不,您必须处理不再使用的图像。 Proper code is: 正确的代码是:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.