【问题标题】:To clear loaded images in a picturebox-c#清除图片框中加载的图像-c#
【发布时间】:2016-09-14 18:24:21
【问题描述】:

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

我面临的问题是当我选择下一个文件夹获取图像进行处理时,之前选择的文件夹图像也显示在图片框中,仅显示下一个文件夹图像,我无法清除以前加载的图像。

具体来说,当我从下拉列表中单击文件夹时,我希望图片框中的特定文件夹图像我不希望之前加载的图像与它们一起。我正在使用 c# 在 VS2013 中工作。

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)
        {

        }

  }
}

【问题讨论】:

  • 欢迎来到Stack Overflow,请阅读How to Ask 并告诉我们您尝试了什么
  • 根据我的简短谷歌搜索,这里有一个建议。在加载新图像之前尝试pictureBox.Image = null;
  • 使用picturebox1.image=null 不能解决问题; @KeyurPATEL
  • PictureBox1.Image = null 是正确的,您的代码都是小写的,C# 语言区分大小写。
  • 正如@SimonPrice 所说,确保添加了正确的行,区分大小写,也可以尝试pictureBox.Image = null; pictureBox.Invalidate();。无效会导致图片框重新绘制。

标签: c# winforms picturebox


【解决方案1】:

您的代码有许多问题。

您要查找的是在加载新文件名之前没有清除alist

所以插入:

    alist.Clear();

之前

    //Get Each files 

还有

   filelength = alist.Count;

在循环之后。添加时无需计算!

另请注意,ArrayList 已被弃用,您应该改用类型安全且功能强大的List&lt;T&gt;

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

当然,名为i 的类变量很愚蠢,而且您还依赖于在comboBox2 中始终包含SelectedItem

并且由于您没有正确处理图像,因此您正在泄漏 GDI 资源。

您可以使用此功能正确加载图像:

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 的引用,然后使用ImageDispose 的引用,最后尝试加载新的引用。

【讨论】:

    猜你喜欢
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2011-08-16
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多