我如何循环FileInfo []并更新Paint事件?

时间:2015-01-29 05:19:20

标签: c# .net winforms

我有这段代码:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pixelscoordinatesinrectangle = new List<Point>();
            pixelscoordinatesinrectangle = pointsAffected.ToList();
            DrawIt = false;
            for (int i = 0; i < trackBar1FileInfo.Length; i++)
            {
                DrawIt = true;
                trackBar1.Value = i;
                LoadPictureAt(trackBar1.Value, sender);
                pictureBox1.Refresh();
                pixelscoordinatesinrectangle = pointsAffected.ToList();
                MessageBox.Show(pixelscoordinatesinrectangle.Count.ToString());
            }
        }

trackBar1FileInfo is FileInfo[] ,其中包含5006个文件,例如

index 0 I see test.gif 
index 1 I see test1.gif 
index 2 test2.gif 
and so on....

在trackBar1滚动事件中,当我左右移动trackBar时,它会更改pictureBox1中的图像。

LoadPictureAt是:

private bool LoadPictureAt(int nIndex, object c)
        {

            {
                bool bRet = false;

                if (nIndex >= 0 && nIndex < trackBar1FileInfo.Length)
                {
                    if (c.Equals(trackBar1))


                        pictureBox1.Load(trackBar1FileInfo[nIndex].FullName);
                    bRet = true;

                }
                if (bitmaps != null)
                {
                    if (nIndex >= 0 && nIndex < bitmaps.Length)
                    {
                        if (c.Equals(trackBar2))
                            pictureBox1.Image = bitmaps[nIndex];
                        bRet = true;
                    }
                }
                return bRet;

            }
        }

我在trackBar1滚动事件中使用方法LoadPictureAt来显示图像。

pixelscoordinatesinrectangle 是列表

在油漆事件中我有:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (cloudPoints != null)
            {
                if (DrawIt)
                {
                    e.Graphics.DrawRectangle(pen, rect);
                    pointsAffected = cloudPoints.Where(pt => rect.Contains(pt));

                    CloudEnteringAlert.pointtocolorinrectangle = pointsAffected.ToList();
                    Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format32bppArgb);
                    CloudEnteringAlert.Paint(e.Graphics, 1, 200, bmp);
                }
            }   
        }

问题是:

  1. 图片框1中的图片在进行循环时不会改变。

  2. List pixelscoordinatesinrectangle永远不会改变,如果它包含203个项目,它将保留203个项目。

  3. 这里的想法是遍历所有图像并更新pictureBox1并执行绘制事件代码,因此每次List pixelscoordinatesinrectangle 的每个图像也会更新。

    现在发生的事情是只有trackBar1移动到0然后逐个向上移动。所有其他人都没有改变。

1 个答案:

答案 0 :(得分:0)

循环在主UI上阻塞。使用单独的线程/后台工作程序进行更新。看看这里,可能重复:Creating a statusfield for users

相关问题