我用C#写了个图片浏览器,用的是picturebox控件,用一个ArrayList把一个文件夹里面的图片的路径存起来,然后就让picturebox显示出来,有两个按钮(前一幅,后一幅),可是,当去到第三幅的时候,就提示内存不足,呜啊。为什么这样子的啊?以下是代码部分:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;namespace 文件夹对话框_文件名
{
    public partial class Form1 : Form
    {
        ArrayList tfilename = new ArrayList();
        string path = null;
        int j = 0;
        int temp;        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {        }        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            { path = folderBrowserDialog1.SelectedPath;
            DirectoryInfo dirinfo = new DirectoryInfo(path);
            FileInfo[] filinfo = dirinfo.GetFiles();
            temp = filinfo.Length-1;
                for(int i=0;i<filinfo.Length;i++)
                { tfilename.Add((path + "\\"+filinfo[i].Name)); }
                pictureBox1.Image=Image.FromFile((tfilename[0]).ToString());
            
            
            
            
            }
        }
        
        private void button1_Click(object sender, EventArgs e)             //前一幅
        {
            try
            {
                if (j == 0)
                    ;
                else
                {
                    j = j - 1;
                    pictureBox1.Image = Image.FromFile((tfilename[j]).ToString());                }
            }
            catch
            {
                }
          
        }        private void button3_Click(object sender, EventArgs e)               //后一幅
        {
            try
            {
                if (j >= temp)
                    ;
                else
                {
                    j = j + 1;
                    pictureBox1.Image = Image.FromFile((tfilename[j]).ToString());                }
            }
            catch
            {
                 }
        
        }
哥哥们有空就帮我看看,谢谢啊,为什么picturebox会内存不足的?有什么好的解决办法吗?

解决方案 »

  1.   

    下一副那个事件太麻烦了,用if直接判断picturebox里的图片索引就可以了,关键就是找出索引然后就能按照索引+1就可以下一张了,你先自己想想吧
      

  2.   

    我根据你的修改了一点典,然后运行了一下, 程序是没有问题的, 可能你没有把文件后缀名打上吧, 这样会把目录下所有文件都存进去,你用我的再试试看using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.IO; namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
           ArrayList tfilename = new ArrayList();
            string path = null;
            int j = 0;
            int temp;
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button2_Click(object sender, EventArgs e)
            {
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                { path = folderBrowserDialog1.SelectedPath;
                DirectoryInfo dirinfo = new DirectoryInfo(path);
                FileInfo[] filinfo = dirinfo.GetFiles("*.jpg");
                temp = filinfo.Length-1;
                    for(int i=0;i <filinfo.Length;i++)
                    { tfilename.Add((path + "\\"+filinfo[i].Name)); }
                    pictureBox1.Image=Image.FromFile((tfilename[0]).ToString());
               
               
               
               
                }
            }
           
            private void button1_Click(object sender, EventArgs e)            //前一幅
            {
                try
                {
                    if (j == 0)
                        ;
                    else
                    {
                        j = j - 1;
                        pictureBox1.Image = Image.FromFile((tfilename[j]).ToString());                }
                }
                catch
                {
                    }
             
            }        private void button3_Click(object sender, EventArgs e)              //后一幅
            {
                try
                {
                    if (j >= temp)
                        ;
                    else
                    {
                        j = j + 1;
                        pictureBox1.Image = Image.FromFile((tfilename[j]).ToString());                }
                }
                catch
                {
                }
            }
        }
    }
      

  3.   

    如果你每次读取目录的时候, 不想保存之前打开的, 或者防止重复保存, 应该先清空ARRAYLIST tfilename.Clear();
    如果没有读取到任何文件, 也应该提示, 这是我最后修改的, 希望能给点帮助
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.IO; namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
           ArrayList tfilename = new ArrayList();
            string path = null;
            int j = 0;
            int temp;
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            bool process = false;        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void button2_Click(object sender, EventArgs e)
            {
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    tfilename.Clear();
                    path = folderBrowserDialog1.SelectedPath;
                    DirectoryInfo dirinfo = new DirectoryInfo(path);
                    FileInfo[] filinfo = dirinfo.GetFiles();
                    if (filinfo.Length > 1)
                    {
                        foreach (FileInfo fi in filinfo)
                        {
                            if (fi.Extension == ".gif" || fi.Extension == ".jpg")  //加上你的所有后缀
                            {
                                temp = filinfo.Length - 1;
                                for (int i = 0; i < filinfo.Length; i++)
                                { tfilename.Add((path + "\\" + filinfo[i].Name)); }
                                pictureBox1.Image = Image.FromFile((tfilename[0]).ToString());
                                process = true;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有照片在这个目录下, 请检查");
                        process = false;                }
                }
            }
           
            private void button1_Click(object sender, EventArgs e)            //前一幅
            {
                if (process)
                {
                    try
                    {
                        if (j != 0)
                        {
                            j = j - 1;
                            pictureBox1.Image = Image.FromFile((tfilename[j]).ToString());                    }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    MessageBox.Show("没有照片在这个目录下, 请检查");
                }
             
            }        private void button3_Click(object sender, EventArgs e)              //后一幅
            {
                if (process)
                {
                    try
                    {
                        if (j > temp)
                        {
                            j = j + 1;
                            pictureBox1.Image = Image.FromFile((tfilename[j]).ToString());                    }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    MessageBox.Show("没有照片在这个目录下, 请检查");
                }
            }
             
        }
    }
      

  4.   

     
    修改楼上代码
     if (fi.Extension == ".gif" || fi.Extension == ".jpg")  //加上你的所有后缀
                            {
                                temp = filinfo.Length - 1;
                                for (int i = 0; i < filinfo.Length; i++)
                                { tfilename.Add((path + "\\" + filinfo[i].Name));  }
                                pictureBox1.Image = Image.FromFile((tfilename[0]).ToString());
                                process = true;
                                j++;//添加...
                            }
      private void button2_Click(object sender, EventArgs e)//后
            {                     
                if (process)
                {
                    try
                    {
                        if (j < temp)///修改