下面函数运行2分钟后发生内存不足错误,使用了using 和disposed()都无效,请高手帮忙!!!!
 private void timer1_Tick(object sender, EventArgs e)
        {
                Graphics g = this.CreateGraphics();
                Image gt = Image.FromFile("d:\\1.bmp");
                Point cc = new Point(100, 100);
                Point dd = new Point(300, 300);
                g.DrawImage(gt, 265, 211, 290, 210);
                gt.Dispose();
                g.Dispose();
                }

解决方案 »

  1.   

    只有一种可能。你的TICK_INTERVAL设得太小了。建议你……不要在TICK事件里写什么需要DISPOSE的东西……而且……我觉得你的这个TICK设计得极为不合理。你有什么,需要每TICK一下都要image.fromfile和 createGraphics?很慢的。drawimage,很慢的!
      

  2.   


    太快了……建议你改一下。你建立一个缓存,把你要读的图片尽可能读好,读到一个地方,比如说数组或者列表里……然后你TICK的时候,从那里去读图……而不是以你现在的方式……还有 Dispose 并没有立即释放内存……只是提交一种请求罢了……GC的效率嘛……我只是觉得,从你提供的代码看,再也看不出别的问题了。
      

  3.   

    把这一句抽出来Image gt = Image.FromFile("d:\\1.bmp"); gt 做成一个成员变量,内存的使用量可以固定一些
      

  4.   

    给你看看全部代码:
     private void button7_Click(object sender, EventArgs e)
            {
                //得到图片文件列表
                DirectoryInfo ddg = new DirectoryInfo("d:\\pic");
                FileInfo[] filelist = ddg.GetFiles();
                foreach (FileInfo file in filelist)
                {
                    filer.Add("d:\\pic\\"+file.Name);
                    
                }
                timer1.Interval = 2000;
                timer1.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                    int len = filer.Count;
                    Graphics g = this.CreateGraphics();
                    Image gt = Image.FromFile(filer[sss]);
                    Point cc = new Point(100, 100);
                    Point dd = new Point(300, 300);
                    g.DrawImage(gt, 265, 211, 290, 210);
                    gt.Dispose();
                    g.Dispose();
                    sss++;
                    if (sss == len)
                        sss = 0;
            }想实现图片浏览效果,可是一会就内存不足了
    试过using()
    {}

    Graphics g = this.CreateGraphics();
    Image gt = Image.FromFile(filer[sss]);
    try{
    }
    finally
    {
    if(g!=null)
    ((IDisposable).g).Dispose();
    if(gt!=null)
    ((IDisposable).gt).Dispose();
    }
    都无效!!!!
      

  5.   

    可能图片加载的慢吧,导致多次调用timer1_Tick 函数重入了吧
      

  6.   

    Image gt = Image.FromFile("d:\\1.bmp");
    这行放到类里面,不要每次定时都执行,也不要gt.Dispose。
      

  7.   

    我用楼主的代码测试了一下,每10毫秒就显示一张图片,内存最大时是200多兆,然后程序会自动释放内存,没有出现内存不足的情况啊。测试文件夹里的图片差不多是200张
    测试代码:
    public partial class Form2 : Form
        {
            ArrayList filer = new ArrayList();       
            int sss = 0;
            public Form2()
            {
                InitializeComponent();
            }
            private void button7_Click(object sender, EventArgs e)
            {
                //得到图片文件列表 
                DirectoryInfo ddg = new DirectoryInfo("F:\\图片\\其它");
                FileInfo[] filelist = ddg.GetFiles();
                foreach (FileInfo file in filelist)
                {
                    filer.Add("F:\\图片\\其它\\" + file.Name); 
                }
                timer1.Interval = 10;
                timer1.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {            int len = filer.Count;
                Graphics g = this.CreateGraphics();
                Image gt = Image.FromFile(filer[sss].ToString());           
                g.DrawImage(gt, 0, 0, this.Size.Width, this.Size.Height);
                sss++;
                if (sss == len)sss = 0;
            }         
        }
      

  8.   

    读到png格式的图片时,会有重叠的现象
      

  9.   

    原来是 图片太大。  呵呵。  其实,即使你用了dispose。 垃圾回收也不是马上就回收的。  所以,当你的刷新频率太快,如果图片本身也比较大。那在垃圾回收之前就内存不足就很正常了。  如果,在每次代码的最后,加上GC。collect();强制回收。不知道能不能行。因为看资料写的是强制回收会影响程序性能。何况你这刷新频率这么高!   一个是导致cpu高负载,一个内存高使用量。 楼主自己测下吧。
      

  10.   

    异常出现在这里
      Image gt = Image.FromFile(filer[sss]);
      

  11.   


    把你的 Image gt = Image.FromFile("d:\\1.bmp"); 放Time_Tick外边..没事拼命打开文件做什么呢.
      

  12.   

    搞矢量图也碰到这样问题,搜了半天看到一个老外的。不过不能解决我的问题,应该能解决你的问题:
    FileStream stream = File.Open(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
       Image OriginalImage = Image.FromStream(stream, true); // Very Important to set true if we close the stream after
       stream.Close();   Image imgClone = (Image)(OriginalImage.Clone());
       Graphics g = Graphics.FromImage(OriginalImage);   g.DrawString("test", DefaultFont, new SolidBrush(Color.Black), 0, 0);
       this.BackgroundImage = OriginalImage;
      

  13.   

    //filer 没有清空,加一句
    filer.Clear();foreach (FileInfo file in filelist)
    {
     filer.Add("F:\\图片\\其它\\" + file.Name); 
     }
      

  14.   


    好主意啊, 一条线程在后台地预读文件,填到一个 List 中; 一条线程显示 List 中的文件, 做点线程保护即可
      

  15.   

    从文件读出的Image,需要先画到另一个Image中然后Dispose。参考代码:Image gt = Image.FromFile(fd.FileName);
    using (Image tmpImag = new Bitmap(gt.Width, gt.Height))
    {
        using (Graphics tmpG = Graphics.FromImage(tmpImag))
        {
            tmpG.DrawImage(gt, 0, 0, gt.Width, gt.Height);
        }
        using (Graphics g = this.CreateGraphics())
        {
            g.DrawImage(gt, 0, 0, this.Size.Width, this.Size.Height);
        }
    }
    gt.Dispose();
      

  16.   

    private void timer1_Tick(object sender, EventArgs e) 
            { 
    timer1.Stop();
                    Graphics g = this.CreateGraphics(); 
                    Image gt = Image.FromFile("d:\\1.bmp"); 
                    Point cc = new Point(100, 100); 
                    Point dd = new Point(300, 300); 
                    g.DrawImage(gt, 265, 211, 290, 210); 
                    gt.Dispose(); 
                    g.Dispose(); 
    timer1.Start();
                    }
    楼主加上这两句试试
      

  17.   

    ...GDI+ 不太精 不过    
                Point cc = new Point(100, 100); 
                    Point dd = new Point(300, 300); 
    是不是要释放啊