如何关闭图片框中打开的图像,以便删除它?

时间:2013-07-22 09:46:48

标签: c#

目前我的表单有一个系统,我可以将表单中的数据保存到应用程序文件夹中的目录,然后通过我已经设置的列表框再次加载它我需要添加一个删除按钮来删除数据出来的问题是如果我打开它它返回一个错误无法删除,因为该文件正在使用继承人我用来保存数据的代码

    private void button3_Click(object sender, EventArgs e)
    {
            using (var writer = new StreamWriter(@"cards\" + CardName.Text + ".card", false))
            {
                int lev = Level.SelectedIndex;
                int race = Race.SelectedIndex;
                int attribute = CardAttribute.SelectedIndex;
                int cardtype = comboBox1.SelectedIndex;
                int monstertype = comboBox2.SelectedIndex;
                int spelltype = comboBox4.SelectedIndex;
                int traptype = comboBox3.SelectedIndex;
                String[] des = CardDescription.Text.Split('\n');
                ImageConverter img_converter = new ImageConverter();
                byte[] bytes = (byte[])img_converter.ConvertTo(IMG, typeof(byte[]));  
                File.WriteAllBytes(@"cards\" + CardName.Text + ".jpg", bytes);
                writer.WriteLine("#ID:" + CardID.Text);
                writer.WriteLine("#lev:" + lev);
                writer.WriteLine("#rac:" + race);
                writer.WriteLine("#att:" + attribute);
                writer.WriteLine("#atk:" + ATK.Text);
                writer.WriteLine("#def:" + DEF.Text);
                writer.WriteLine("#pic:" + CardName.Text + ".jpg");
                writer.WriteLine("#ctp:" + cardtype);
                writer.WriteLine("#mtp:" + monstertype);
                writer.WriteLine("#stp:" + spelltype);
                writer.WriteLine("#ttp:" + traptype);
                writer.WriteLine("#gol:" + checkBox1.Checked);
                writer.WriteLine("#nam:" + CardName.Text);
                foreach (string l in des)
                {
                    writer.WriteLine("#des:" + l);
                }
            }
            ListUpdate();
        }

加载数据

    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        string open = @"cards\" + listBox1.SelectedItem.ToString() + ".card";
        var reader = new StreamReader(File.OpenRead(open));
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            if (line.StartsWith("#ID:"))
            {
                CardID.Text = line.Substring(4);
            }
            if (line.StartsWith("#lev:"))
            {
                string lev = line.Substring(5);
                Level.SelectedIndex = Convert.ToInt32(lev);
            }
            if (line.StartsWith("#rac:"))
            {
                string rac = line.Substring(5);
                Race.SelectedIndex = Convert.ToInt32(rac);
            }
            if (line.StartsWith("#att:"))
            {
                string att = line.Substring(5);
                CardAttribute.SelectedIndex = Convert.ToInt32(att);
            }
            if (line.StartsWith("#atk:"))
            {
                ATK.Text = line.Substring(5);
            }
            if (line.StartsWith("#def:"))
            {
                DEF.Text = line.Substring(5);
            }
            if (line.StartsWith("#ctp:"))
            {
                string ctp = line.Substring(5);
                comboBox1.SelectedIndex = Convert.ToInt32(ctp);
            }
            if (line.StartsWith("#mtp:"))
            {
                string mtp = line.Substring(5);
                comboBox2.SelectedIndex = Convert.ToInt32(mtp);
            }
            if (line.StartsWith("#stp:"))
            {
                string stp = line.Substring(5);
                comboBox4.SelectedIndex = Convert.ToInt32(stp);
            }
            if (line.StartsWith("#ttp:"))
            {
                string ttp = line.Substring(5);
                comboBox3.SelectedIndex = Convert.ToInt32(ttp);
            }
            if (line.StartsWith("#gol:"))
            {
                string gold = line.Substring(5);
                checkBox1.Checked = Convert.ToBoolean(gold);
            }
            if (line.StartsWith("#nam:"))
            {
                CardName.Text = line.Substring(5);
            }
            if (line.StartsWith("#des:"))
            {
                des += line.Substring(5) + Environment.NewLine;
                CardDescription.Text = des;
            }
            if (line.StartsWith("#pic:"))
            {
                if (File.Exists(@"cards\" + line.Substring(5)))
                {
                    IMG = Image.FromFile(@"cards/" + line.Substring(5));
                }
            }
        }  
        des = "";
        GenerateCard();
    }

并删除它

    private void button4_Click(object sender, EventArgs e)
    {
        string list = listBox1.SelectedItem.ToString();
        if (File.Exists(@"cards\" + list + ".card"))
        {
            File.Delete(@"cards\" + list + ".card");
        }
        if (File.Exists(@"cards\" + list + ".jpg"))
        {
            File.Delete(@"cards\" + list + ".jpg");
        }
        ListUpdate();
    }

3 个答案:

答案 0 :(得分:1)

好的,PictureBox正在使用您要删除的图片的问题......对吗? 因此,我们可以将图片加载到PictureBox

,而不是使用本地驱动器中的图片
public Bitmap LoadBitmap(string path)
{
    if (File.Exists(path))
    {
        // open file in read only mode
        using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        // get a binary reader for the file stream
        using (BinaryReader reader = new BinaryReader(stream))
        {
            // copy the content of the file into a memory stream
            var memoryStream = new MemoryStream(reader.ReadBytes((int)stream.Length));
            // make a new Bitmap object the owner of the MemoryStream
            return new Bitmap(memoryStream);
        }
    }
    else
    {
        MessageBox.Show("Error Loading File.", "Error!", MessageBoxButtons.OK);
        return null;
    }
}

要使用它,只需致电:

picturebox1.Image = LoadBitmap("LOCATION");

现在您可以在不锁定的情况下删除图片。但要注意,`PictureBox不会被清除。你必须手动清除它。

我希望它有所帮助。 ;)

答案 1 :(得分:1)

Nathanael Jones的博文"20 Image Resizing Pitfalls"正好描述了这个问题:

  

“按文件名打开BitmapImage会导致文件在Bitmap实例期间被锁定。

这是他建议的解决方案:

  

“您可以使用FileStream来避免锁定,将其克隆到MemoryStream,处置FileStream,然后使用.Tag属性进行跟踪并稍后处理MemoryStream。“

答案 2 :(得分:0)

请改用Load方法。 例如:pictureBox1.Load(图像的路径); 使用此功能,在关闭应用程序之前删除图像或文件夹没有任何问题 希望这有帮助