Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Initially I will be loading images(say 20 images) into a picturebox from a specified folder through selection from combobox dropdown, they get loaded normally into the picturebox.

The problem I am facing is when I select the next folder to acquire the image for processing, the previously selected folders images are also displayed in the picturebox after its count only the next folders images are displayed, I am unable to clear the previously loaded images.

To be specific, when I click on a folder from dropdown I want the particular folders image inside the picturebox I don't want the previously loaded images along with them. Am working in VS2013 with c#.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ArrayList alist = new ArrayList();
        int i = 0;
        int filelength = 0;
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo di = new      DirectoryInfo(@"C:UsersArunDesktopscanned");
            DirectoryInfo[] folders = di.GetDirectories();
            comboBox1.DataSource = folders;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (i + 1 < filelength)
            {
                pictureBox1.Image = Image.FromFile(alist[i + 1].ToString());
                i = i + 1;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {

            if (i - 1 >= 0)
            {

                pictureBox1.Image = Image.FromFile(alist[i - 1].ToString());
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                i = i - 1;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected = comboBox1.SelectedItem.ToString();
            String fullpath = Path.Combine(@"C:UsersArunDesktopscanned", selected);
            DirectoryInfo di1 = new DirectoryInfo(fullpath);
            DirectoryInfo[] folders1 = di1.GetDirectories();
            comboBox2.DataSource = folders1;

        }

        private void button9_Click(object sender, EventArgs e)
        {

            string selected1 = comboBox1.SelectedItem.ToString();
            string selected2 = comboBox2.SelectedItem.ToString();

                        //Initially load all your image files into the array list when form load first time
            System.IO.DirectoryInfo inputDir = new System.IO.DirectoryInfo(Path.Combine(@"C:UsersArunDesktopscanned", selected1, selected2)); //Source image folder path


            try
            {


                if ((inputDir.Exists))
                {

                    //Get Each files 
                    System.IO.FileInfo file = null;
                    foreach (System.IO.FileInfo eachfile in inputDir.GetFiles())
                    {
                        file = eachfile;
                        if (file.Extension == ".tif")
                        {
                            alist.Add(file.FullName); //Add it in array list
                            filelength = filelength + 1;
                        }
                        else if(file.Extension == ".jpg")
                        {

                            alist.Add(file.FullName); //Add it in array list
                            filelength = filelength + 1;
                        }
                    }

                    pictureBox1.Image = Image.FromFile(alist[0].ToString());  //Display intially first image in picture box as sero index file path
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    i = 0;

                }
            }
            catch (Exception ex)
            {

            }

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.D)
            {
                if (i + 1 < filelength)
                {
                    pictureBox1.Image = Image.FromFile(alist[i + 1].ToString());
                    i = i + 1;
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                }
            }
            else if(e.KeyCode == Keys.A)
            {
                if (i - 1 >= 0)
                {

                    pictureBox1.Image = Image.FromFile(alist[i - 1].ToString());
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    i = i - 1;
                }
            }
        }


        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
316 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your code has many issues.

The one you are looking for is that you don't clear the alist before loading new file names.

So insert:

    alist.Clear();

before

    //Get Each files 

And also

   filelength = alist.Count;

after the loop. No need to count while adding!

Also note that ArrayList is pretty much depracated and you should use the type-safe and powerful List<T> instead:

List<string> alist = new List<string>();

Of course a class variable named i is silly and you are also relying on always having a SelectedItem in the comboBox2.

And since you are not properly Disposing of the Image you are leaking GDI resources.

You can use this function for properly loading images:

void loadImage(PictureBox pbox, string file)
{
    if (pbox.Image != null)
    {
        var dummy = pbox.Image;
        pbox.Image = null;
        dummy.Dispose();
    }
    if (File.Exists(file)) pbox.Image = Image.FromFile(file);
}

It first creates a reference to the Image, then clears the PictureBox's reference, then uses the reference to Dispose of the Image and finally tries to load the new one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...