C# 如何关闭在picturebox中打开的图像以便删除它?
目前,我的表单有一个系统,我可以将表单中的数据保存到应用程序文件夹中的一个目录中,然后通过我现在设置的列表框再次加载它。我需要添加一个删除按钮来删除文件夹中的数据。问题是,如果我打开它,它会返回一个错误,无法删除,因为文件正在使用中。这是我的代码用于保存数据C# 如何关闭在picturebox中打开的图像以便删除它?,c#,C#,目前,我的表单有一个系统,我可以将表单中的数据保存到应用程序文件夹中的一个目录中,然后通过我现在设置的列表框再次加载它。我需要添加一个删除按钮来删除文件夹中的数据。问题是,如果我打开它,它会返回一个错误,无法删除,因为文件正在使用中。这是我的代码用于保存数据 private void button3_Click(object sender, EventArgs e) { using (var writer = new StreamWriter(@"card
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();
}
好的,
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”不会被清除。你必须手动清除它
我希望有帮助 好的,
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”不会被清除。你必须手动清除它
我希望有帮助 Nathanel Jones的博客文章准确地描述了这个问题: “按文件名打开
位图
或图像
将导致文件在位图
实例期间被锁定
这是他建议的解决方案:
通过使用文件流
,将其克隆到内存流
,处理文件流
,然后使用.Tag
属性跟踪并稍后处理内存流
,可以避免锁定
Nathanel Jones的博客文章正好描述了这个问题: “按文件名打开
位图
或图像
将导致文件在位图
实例期间被锁定
这是他建议的解决方案:
通过使用文件流
,将其克隆到内存流
,处理文件流
,然后使用.Tag
属性跟踪并稍后处理内存流
,可以避免锁定
改为使用加载方法。 例如:pictureBox1.Load(图像的路径); 通过使用此功能,您在关闭应用程序之前删除图像或文件夹不会有任何问题
希望这有助于使用加载方法。 例如:pictureBox1.Load(图像的路径); 通过使用此功能,您在关闭应用程序之前删除图像或文件夹不会有任何问题
希望这有帮助这部分工作正常我在这部分遇到一个错误我假设这是一个你忘记发布removepic()的函数;没问题,我只是删除了它现在我只需要知道如何在另一个文件上做同样的事情,我将被设置,我不想清除picturebox程序所做的是它根据图片路径中的表单值juzt放置来隐藏图像。这样调用:'YOURPICTUREBOX.image=LoadBitmap(“图像路径”);在“删除”按钮上,单击“juzt do”文件。删除(“图像路径”);:)图像部分现在可以正常工作了,多亏了你。我的意思是.card文件。它部分可以工作。我在这部分上遇到一个错误。我假设这是一个函数,你忘记发布removepic();没问题,我只是删除了它现在我只需要知道如何在另一个文件上做同样的事情,我将被设置,我不想清除picturebox程序所做的是它根据图片路径中的表单值juzt放置来隐藏图像。这样调用:'YOURPICTUREBOX.image=LoadBitmap(“图像路径”);在“删除”按钮上,单击“juzt do”文件。删除(“图像路径”);:)图像部分现在可以很好地工作了,多亏了你,我指的是.card文件