【问题标题】:Parameter is not valid when i try to use bitmap当我尝试使用位图时参数无效
【发布时间】:2014-08-28 23:01:57
【问题描述】:

我正在尝试创建一个在 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 上绘制火车地图(线路、车站、...),尺寸为x=Ay=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)

【问题讨论】:

  • 你试过这个new Bitmap(pictureBoxonlineTrain.Width, pictureBoxonlineTrain.Height))
  • @HassanNisar 我在我的代码中使用了它
  • 尝试从using (map = ... 中删除using - 我认为当图片框仍在尝试绘制位图时正在处理它。
  • 你处理了它,小心盲目地应用 using 语句。
  • @Blorgbeard 我之前在这个问题中删除了 using 语句,但是我的内存不足异常,请你看看这个:stackoverflow.com/questions/24589544/…

标签: c# multithreading image bitmap picturebox


【解决方案1】:

由于内存不足异常,我必须处理它

不,您必须处理不再使用的图像。正确的代码是:

   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;

【讨论】:

  • 它工作了几分钟后我在这里遇到了同样的错误:Bitmap map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height);
  • 我在您的代码中添加了一些细节,现在它可以正常工作了
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多