Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动画不是';picbox无法工作,因为picbox无法';t重置且任务不';睡不着_C#_Animation_Bitmap - Fatal编程技术网

C# 动画不是';picbox无法工作,因为picbox无法';t重置且任务不';睡不着

C# 动画不是';picbox无法工作,因为picbox无法';t重置且任务不';睡不着,c#,animation,bitmap,C#,Animation,Bitmap,我的代码就在这里: 编辑:源代码: private void btnAnimate_Click(object sender, EventArgs e) { Bitmap[] circle = new Bitmap[300]; for (int i = 0; i < circle.Length; i++) { circle[i] = new Bitmap(2

我的代码就在这里: 编辑:源代码:

        private void btnAnimate_Click(object sender, EventArgs e)
        {
            Bitmap[] circle = new Bitmap[300];
            for (int i = 0; i < circle.Length; i++)
            {
                circle[i] = new Bitmap(260, 266);
            }
            double r = 25; // radius
            double rr = Math.Pow(r, 2); // r^2

            int h = 25; 
            // x value of centre of circle is represented with h and y value is represented with k
            for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
            {
                
                for (int x = -h; x <= h + r; x++)
                {
                    for (int y = -k; y <= k + r; y++)
                    {
                        if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
                        // if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
                        {
                           
                            circle[k - 25].SetPixel(x, y, Color.Red);
                            
                        }
                    }
                }
            }
            for (int l = 25; l <= 100; l+= 25)
            {
                picBox.Image = circle[l - 25];
                btnAnimate.Text = "circle" + (l/25);
                System.Threading.Thread.Sleep(1000);
            }
        }
我希望它将按钮文本更改为1、2、3和4来计算每次迭代,但它也没有这样做。最后只显示4

你认为问题是什么

解决方案:

            

            double r = 25; // radius
            double rr = Math.Pow(r, 2); // r^2

            int h = 25; 
            // x value of centre of circle is represented with h and y value is represented with k
            for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
            {
                Bitmap circle = new Bitmap(260, 266);
                for (int x = -h; x <= h + r; x++)
                {
                    for (int y = -k; y <= k + r; y++)
                    {
                        if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
                        // if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
                        {
                           
                            circle.SetPixel(x, y, Color.Red);
                            
                        }
                    }
                }
                picBox.Image = circle;
                picBox.Update();
                System.Threading.Thread.Sleep(100);
            }


双r=25;//半径
双rr=数学功率(r,2);//r^2
int h=25;
//圆心的x值用h表示,y值用k表示
对于(int k=25;k您有两个问题:

  • 您没有在每次迭代中清除
    位图圆
    绘制的圆,因此您是在上一个绘制的圆的基础上构建的,因此它看起来像一个带轨迹的下降圆。您可以通过在第一个for循环的内部而不是外部定义
    位图圆
    来解决此问题

  • 它无法在线程睡眠之间显示新图像,因为您没有告诉控件以可视化方式刷新自身。请在设置
    picBox.image=circle
    后调用
    picBox.Update()
    picBox.refresh()


  • 我修复了不重置的问题。我意识到这是因为我没有重置位图对象。所以我只是创建了一个300位图数组,每次迭代它都会创建一个新的位图。现在唯一的问题是threading.sleep。在每次睡眠之间,它都无法显示图像。它将等待100毫秒,不显示图像,然后继续,直到显示为止到达最终图像。这是怎么回事?在.NET中有很多UI环境。您使用哪一个?请使用标记来指示这些内容。不要将源代码作为屏幕截图发布。将其作为文本包含在文章中;否则,您只会使其他人难以测试您的代码并帮助您。您应该使用计时器,或r切换到异步/等待模式。你不能使用
    Thread.Sleep
    在UI上制作动画,因为你正在休眠的线程是UI更新线程,这意味着在它休眠时没有任何东西会刷新。噢,哇,非常感谢!我意识到了第一个问题,但你的解决方案比创建300位MAB对象要有效得多哈哈。第二个很有用。谢谢你,伙计。不客气!作为补充提示,我建议你打电话给
    circle.Dispose()
    当你使用完它,否则你会出现内存泄漏。很高兴我能提供帮助。通过
    update
    Refresh
    强制不定期的UI更新通常是一种不好的做法。这通常是由于
    Thread.Sleep
    或其他阻塞操作,UI已经窒息的迹象,这使得UI完全无法响应ive。它不仅会阻止所有鼠标和其他事件,还会阻止常规的
    WM_PAINT
    事件来完成它的工作。您应该使用计时器,只需在其
    Tick
    事件中设置新图像。或者,更好的是,只需调用
    picBox.Invalidate()
    中勾选
    ,然后用
    Graphics.FillEllipse在
    Paint
    中自己画球。它将比极其缓慢的
    设置像素
    快得多。
                
    
                double r = 25; // radius
                double rr = Math.Pow(r, 2); // r^2
    
                int h = 25; 
                // x value of centre of circle is represented with h and y value is represented with k
                for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
                {
                    Bitmap circle = new Bitmap(260, 266);
                    for (int x = -h; x <= h + r; x++)
                    {
                        for (int y = -k; y <= k + r; y++)
                        {
                            if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
                            // if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
                            {
                               
                                circle.SetPixel(x, y, Color.Red);
                                
                            }
                        }
                    }
                    picBox.Image = circle;
                    picBox.Update();
                    System.Threading.Thread.Sleep(100);
                }