位图ArgumentExeption:参数无效。位图循环内存问题

时间:2016-03-21 13:15:01

标签: c# memory graphics bitmap argumentexception

我试图创建一个简单的渲染程序,它绘制一个位图,然后在picturebox中显示并重复。看起来位图在系统中积累了很多内存?我尝试(和找到)的解决方案不起作用,例如dispose()file stream等。

我可以运行一次这个程序,但是当它在一个循环中运行多次时它不会工作。

如果我尝试dispose(),位图会显示一个奇怪的图像。

  1. 如何使我的循环工作,计算,然后在位图上绘制,然后在图片框中显示位图,然后重复。
  2. :)

    代码:

        private void renderobjekter(int type)
        {
    
            Bitmap bm = new Bitmap(1400, 800);
    
            using (Graphics gr = Graphics.FromImage(bm))
            {
    
                for (int objlist = 0; objlist < objektertype[type].GetLength(0); objlist++)
            {
                double d = 0;
                double[,] resultobj = new double[objekter[objektertype[type][objlist]].GetLength(0), rendermatrix.GetLength(1)];
                for (int i = 0; i < resultobj.GetLength(0); i++)
                {
                    for (int j = 0; j < resultobj.GetLength(1); j++)
                    {
                        d = 0;
                        for (int k = 0; k < rendermatrix.GetLength(0); k++)
                        {
                            d = rendermatrix[k, j] * objekter[objektertype[type][objlist]][i, k] + d;
                        }
                        resultobj[i, j] = d;
                    }
                }
    
                for (int i = 0; i < resultobj.GetLength(0); i++)
                {
                    for (int j = 0; j < resultobj.GetLength(1); j++)
                    {
                        resultobj[i, j] = resultobj[i, j] / resultobj[i, resultobj.GetLength(1) - 1];
                    }
                }
    
                /*using (Graphics gr = Graphics.FromImage(bm))
                {
                    for (int i = 0; i < resultobj.GetLength(0); i++)
                    {
                        if (resultobj[i, 0] < 1 && resultobj[i, 0] > -1 && resultobj[i, 1] < 1 && resultobj[i, 1] > -1 && resultobj[i, 2] < 1 && resultobj[i, 2] > -1)
                        {
    
                            double x = (resultobj[i, 0] + 1) * ((double)pictureBox1.Width / (double)2);
                            double y = pictureBox1.Height - (resultobj[i, 1] + 1) * ((double)pictureBox1.Height / (double)2);
    
                            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                            gr.DrawEllipse(pen1, (float)x, (float)y, 1, 1);
    
                        }
                        else
                        {
    
                        }
                    }
                }*/
                for (int j = 0; j < navigation[objektertype[type][objlist]].GetLength(0); j++)
                {
                    int s_p = navigation[objektertype[type][objlist]][j, 0];
                    int n_p = navigation[objektertype[type][objlist]][j, 1];
                    double xp = (resultobj[s_p, 0] + 1) * ((double)pictureBox1.Width / (double)2);
                    double yp = pictureBox1.Height - (resultobj[s_p, 1] + 1) * ((double)pictureBox1.Height / (double)2);
                    double xn = (resultobj[n_p, 0] + 1) * ((double)pictureBox1.Width / (double)2);
                    double yn = pictureBox1.Height - (resultobj[n_p, 1] + 1) * ((double)pictureBox1.Height / (double)2);
    
                    if (resultobj[s_p, 0] < -1.2 || resultobj[s_p, 1] > 1.2 & resultobj[n_p, 0] < -1.2 || resultobj[n_p, 1] > 1.2)
                    {
    
                    }
                    else {
                        gr.DrawLine(pen2, (float)xp, (float)yp, (float)xn, (float)yn);
                    }
                }
            }
            }
        pictureBox1.Image = bm;
        }
    
        private void timer()
        {
    
            for (int i = 0; i < 10000; i++) {
    
                transformer(1, 1);
                renderobjekter(1);
                //abc();
                label1.Text = i + " ";
            }
    

0 个答案:

没有答案