我有这个代码:
_fi = new DirectoryInfo (subDirectoryName).GetFiles("*.bmp");
label15.Text = _fi.Length.ToString();
for (int i = 0; i < myNumbers.Count; i++)
{
if (myNumbers[i] >= max_min_threshold)
{
FileName = i.ToString("D6") + ".bmp";
if (File.Exists(subDirectoryName + "\\" + FileName))
...
}
}
现在 _fi 包含 2255 个索引;在每个索引中都有一个文件名,例如在 index[0] 我看到:{000001.bmp}
In index[1] {000002.bmp}
然后我运行了 myNumbers,其中每个索引都包含 2256 个索引。例如在 index[0] 中有 225000 在 index[1] 中有 223000 等等。
我想要做的是使用此循环或其他方式以及每次迭代将文件名从 _fi 变量获取到 FileName 变量中。
所以在第一个变量中 FileName 将是 000001.bmp 在第二次迭代中 FileName 将是 000002.bmp 等等。
问题是 FileName 现在在第一次迭代中将是 000000.bmp ,它在硬盘中不存在。我在 _fi 中看到第一个文件是 000001.bmp
那么我怎样才能让它工作呢?
编辑:
试图这样做:
if (i == _fi.Length)
{
break;
}
FileName = (i + 1).ToString("D6") + ".bmp";
问题是,休息后它进入这部分:
button5.Enabled = true;
myTrackPanelss1.trackBar1.Maximum = counter;
myTrackPanelss1.trackBar1.Value = 0;
setpicture(0);
现在设置图片(0);将在 pictureBox1 中显示帧/图像编号 1,即文件:000001.bmp 这是 setpicture 函数:
private void setpicture(int indx)
{
if (_fi == null)
{
pictureBox1.Image = Lightnings_Extractor.Properties.Resources.Weather_Michmoret;
button5.Enabled = false;
label5.Visible = true;
}
else
{
if (indx >= 0 && indx <= myTrackPanelss1.trackBar1.Maximum && _fi.Length > indx)
{
try
{
label19.ForeColor = Color.Red;
fileToolStripMenuItem.Enabled = true;
label19.Visible = false;
label20.Visible = false;
label14.Visible = true;
label15.Visible = true;
label8.Visible = true;
label9.Visible = true;
myTrackPanelss1.trackBar1.Enabled = true;
using (FileStream fs = new FileStream(_fi[indx].FullName, FileMode.Open))
{
this.label8.Visible = true;
this.label9.Visible = true;
this.label9.Text = _fi[indx].Name;
Image img = null;
Bitmap bmp = null;
Image imgOLd = null;
try
{
img = Image.FromStream(fs);
bmp = new Bitmap(img);
imgOLd = this.pictureBox1.Image;
this.pictureBox1.Image = bmp;
if (imgOLd != null)
imgOLd.Dispose();
img.Dispose();
img = null;
}
catch
{
if (img != null)
img.Dispose();
if (bmp != null)
bmp.Dispose();
if (imgOLd != null)
imgOLd.Dispose();
}
}
}
catch
{
button1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label11.Visible = false;
label12.Visible = false;
checkBox2.Enabled = false;
label19.Visible = true;
label19.ForeColor = Color.Green;
label19.Text = "The Selected Directory Is Access Denied";
label20.Visible = true;
label20.ForeColor = Color.Green;
label20.Text = "Please Set A Different Directory";
fileToolStripMenuItem.Enabled = false;
label14.Visible = false;
label15.Visible = false;
label8.Visible = false;
label9.Visible = false;
myTrackPanelss1.trackBar1.Enabled = false;
timer2.Stop();
return;
}
}
else
{
Image imgOLd = this.pictureBox1.Image;
//this.pictureBox1.Image = null;
if (imgOLd != null)
{
imgOLd.Dispose();
imgOLd = null;
}
Application.DoEvents();
}
}
}
在线上:
using (FileStream fs = new FileStream(_fi[indx].FullName, FileMode.Open))
我现在遇到了异常,这不是在我休息之前;如果 i == _fi.Length 但在我这样做之后,我在这条线上遇到异常,它移动到捕获区域并扔给我:
该进程无法访问文件“D:\New folder (7)\MVI_3041.MOV_Automatic\000001.bmp”,因为它正被另一个进程使用。
System.IO.IOException was caught
Message=The process cannot access the file 'D:\New folder (7)\MVI_3041.MOV_Automatic\000001.bmp' because it is being used by another process.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Extracting_Frames.Form1.setpicture(Int32 indx) in D:\C-Sharp\Extracting_Frames\Extracting_Frames\Extracting_Frames\Form1.cs:line 508
InnerException:
我不知道为什么现在我得到了这个以前没有的异常。