【问题标题】:How do i loop over FileInfo[] and update the Paint event?我如何循环 FileInfo[] 并更新 Paint 事件?
【发布时间】:2015-01-29 05:19:19
【问题描述】:

我有这个代码:

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 时,它会更改图片框 1 中的图像。

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. pictureBox1 中的图像在循环时没有变化。

  2. List pixelcoordinatesinrectangle 永远不会改变,如果它有 203 个项目,它将保持 203 个项目。

这里的想法是循环所有图像并更新pictureBox1并执行绘制事件代码,因此每次每个图像列表pixelscoordinatesinrectangle也会更新。

现在发生的情况是,只有 trackBar1 移动到 0,然后一个接一个地向上移动。其他的都没有变化。

【问题讨论】:

  • 我看到使用断点,它每次都会到达绘画事件,但点列表永远不会改变相同数量的相同像素坐标。循环中的某些内容可能与我提供给 LoadPictureAt 方法的值有关。在 Paint 事件中,它应该在每个图像上绘制矩形并获取其中的像素坐标。但实际上 List 永远不会改变,pictureBox 中的图像也永远不会改变,只是 trackBar1 的值改变。
  • 问题可能是您从主线程运行所有内容,因此即使您在图片框上调用Refresh,应用程序也没有时间实际重绘它。但我还没有测试过你的代码,所以很可能是别的东西。
  • 在 Paint 事件处理程序中放置断点可能会出现问题 - 当 Visual Studio 调试器接管 Windows UI 时,您要求 Windows 暂时停止更新即将成为窗口显示一部分的位并显示调试信息。另一种调试技术是将日志语句放置在 Paint 事件处理程序中。

标签: c# .net winforms


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-12
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2018-09-16
    • 2019-05-15
    • 1970-01-01
    相关资源
    最近更新 更多