清除图片框中加载的图像-c# [英] To clear loaded images in a picturebox-c#

查看:100
本文介绍了清除图片框中加载的图像-c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我将通过从组合框下拉菜单中进行选择,将指定文件夹中的图像(比如 20 张图像)加载到图片框中,它们会正常加载到图片框中.

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.

具体来说,当我从下拉菜单中单击文件夹时,我想要图片框内的特定文件夹图像,我不想要以前加载的图像以及它们.我正在使用 c# 在 VS2013 中工作.

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:\Users\Arun\Desktop\scanned");
            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:\Users\Arun\Desktop\scanned", 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:\Users\Arun\Desktop\scanned", 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)
        {

        }

  }
}

推荐答案

您的代码有许多问题.

您正在寻找的是在加载新文件名之前没有清除alist.

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

所以插入:

    alist.Clear();

之前

    //Get Each files 

还有

   filelength = alist.Count;

循环之后.添加时无需计数!

after the loop. No need to count while adding!

另请注意,ArrayList 已经过时了,您应该使用类型安全且功能强大的 List 代替:

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>();

当然,一个名为 i 的类变量是愚蠢的,而且你还依赖于在 comboBox2 中总是有一个 SelectedItem.

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

而且由于您没有正确处理图像,您正在泄漏 GDI 资源.

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);
}

它首先创建对 Image 的引用,然后清除 PictureBox 的引用,然后使用对 Dispose 的引用code>Image 并最终尝试加载新的.

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.

这篇关于清除图片框中加载的图像-c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆