日期:2014-05-18  浏览次数:21269 次

请教:线程间操作无效: 从不是创建控件“pictureBox1”的线程访问它。
在一个线程中定义pictureBox1不断的从e盘获取图片然后进行计算但是老是出现线程间操作无效: 从不是创建控件“pictureBox1”的线程访问它。这个问题,请高手指教一下。其中public void jsplus(int x1, int x2, int y1, int y2, Image img)是计算图片颜色平均值然后在图片中添加些物体把添加物体的那部分区域涂色。代码如下:

 private void button1_Click(object sender, EventArgs e)
  {
  Thread thread = new Thread(CrossThreadFlush);
  thread.IsBackground = true;
  thread.Start();

  }

  private void CrossThreadFlush()
  {
  //将代理绑定到方法
  FlushClient fc = new FlushClient(ThreadFuntion);
  while (true)
  {
  //将sleep和无限循环放在等待异步的外面
  Thread.Sleep(1000);
  ThreadFuntion();
  }

  }

  private void ThreadFuntion()
  {
  //pictureBox1.Image = Image.FromFile("e:\\a.bmp", true);
  //this.pictureBox1.Refresh();
  //try
  //{
  String path = String.Format("e:\\a.bmp");
  Bitmap bitmp1;
  Bitmap bitmp = new Bitmap(Image.FromFile("e:\\a.bmp", true));
  bitmp1 = bitmp;
  this.pictureBox1.Image = bitmp1;
  this.pictureBox1.Refresh();
  jsplus(st1, st2, st3, st4, this.pictureBox1.Image); //其中的参数是图片中的左边
  jsplus(st5, st6, st7, st8, this.pictureBox1.Image);
  jsplus(st9, st10, st11, st12, this.pictureBox1.Image);
  jsplus(st13, st14, st15, st16, this.pictureBox1.Image);
  jsplus(st17, st18, st19, st20, this.pictureBox1.Image);
  // MessageBox.Show(p1 + "\n" + p2 + "\n" + p3 + "\n" + p4 + "\n" + p5); 
  //}
  //catch (Exception e) {
  // MessageBox.Show(e.Message);
  //}
  }

public void jsplus(int x1, int x2, int y1, int y2, Image img)
  {
  g = this.pictureBox1.CreateGraphics();

  int r = 0, g1 = 0, b = 0, t = 0;

  if (img != null)
  {

  Bitmap process = (Bitmap)img;
  Color pixel;
  for (int x = x1; x < x2 + 1; x++)
  {
  for (int y = y1; y < y2 + 1; y++)
  {

  pixel = process.GetPixel(x, y);


  r += pixel.R;
  g1 += pixel.G;
  b += pixel.B;


  }
  }

  t = (r + g1 + b) / (3 * (x2 - x1) * (y2 - y1));
  //string m = t.ToString();

  // MessageBox.Show(m);




  }

  int vcr1 = 0, vcr2 = 0, vcr3 = 0, vcr4 = 0, vcr5 = 0, vcr6 = 0, vcr7 = 0, vcr8 = 0, vcr9 = 0, vcr10 = 0, vcr11 = 0, vcr12 = 0, vcr13 = 0, vcr14 = 0, vcr15 = 0, vcr16 = 0;
  // string p1=null;
  if (x1 > st1 - 1 & x2 < st2 + 1 & y1 > st3 - 1 & y2 < st4 + 1)
  {

  vcr1 = Math.Abs(t - sc1);
  if (vcr1 >= 4)
  {<