c#实现巴恩斯利蕨,运行出问题,求助解决

代码如下,一个菜单栏一个picturebox控件,一运行就卡死,显示是Bitmap myBitmap = new Bitmap(pictureBox1.Width, pictureBox1出了问题,但是不完全知道怎么改啊哭
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
int r;
double x, y;
x = 0.0; y = 0.0;

        Random ra = new Random(0);//种子

        for (int i = 0; i < 50000; i++)
        {
            Random ran = new Random();
            r = ran.Next()%100;//随机数

            if (r< 0)
            {
                x = 0;
                y = 0.16 * y;
            }
            else if (r<8)
            {
                x = 0.2 * x - 0.26 * y;
                y = 0.23 * x + 0.22 * y + 1.6;
            }
            else if (r<15)
            {
                x = -0.15 * x + 0.28 * y;
                y = 0.26 * x + 0.24 * y + 0.44;
            }
            else
            {
                x = 0.85 * x + 0.04 * y;
                y = -0.04 * x + 0.85 * y + 1.6;
            }
            Bitmap myBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            myBitmap.SetPixel((int)((x / 10)), (int)((y / 10 - 0.3)), Color.Green);

            pictureBox1.Image = myBitmap;
        }

    }

每次循环都要重新分配空间,当然很卡。把“Bitmap myBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);”放到循环之外就好了。

多开一个线程试试,不要在主线程中操作picturebox控件。

回头看看单例模式 创建一个对象 然后在循环过程中一次次的重新利用这个对象,这样就避免了一次次的循环中重复创建同一类型的对象,两者相比较资源消耗是指数型