我创建了一个对话框程序,当我为一个文件夹的任何图片创建图片框时,当我关闭此对话框时,我想删除图片框,但是,我收到一个异常,这个异常告诉我:文件0.jpg 被另一个进程使用
但是,所以,我试图处理掉所有的图片框......当然,据我所知,我已经尝试了所有可能的事情......
所以,我的示例代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace delFolder
{
public partial class FormPictureView : Form
{
private PictureBox pbSel = null;
public FormPictureView()
{
InitializeComponent();
AddPictureBox();
}
private void AddPictureBox()
{
int linha = 0;
int x = 10, y = 10;
string pngOutputPath = @"images\";
string[] files = Directory.GetFiles(pngOutputPath);
for (int i = 0; i < files.Length; i++)
{
if (linha == 2)
{
x = 10;
y += 360;
linha = 0;
}
PictureBox pb = new PictureBox();
pb.Name = string.Format("pb{0}", i);
pb.Size = new Size(236, 321);
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.Image = Image.FromFile(string.Format("{0}{1}.jpg", pngOutputPath, i));
pb.BackColor = Color.White;
pb.Click += new EventHandler(pb_Click);
pb.Tag = string.Format("{0}{1}.jpg", pngOutputPath, i);
pb.Location = new System.Drawing.Point(x, y);
panel1.Controls.Add(pb);
x += 280;
linha++;
}
}
private void pb_Click(object sender, EventArgs e)
{
if (pbSel != null)
pbSel.BorderStyle = BorderStyle.None;
PictureBox pb = (PictureBox)sender;
pb.BorderStyle = BorderStyle.FixedSingle;
pbSel = pb;
MessageBox.Show(pb.Tag.ToString());
}
}
}
并且,此对话框是主窗体的 MDIParent ...,并且,当我关闭此对话框时,我尝试删除图片框,但是,这是不可能的 :( 我该如何解决这个问题?