2012-03-01 11 views
0

私はデータベースに画像を保存するボタンがあり、そのディレクトリを削除する機能はデータベースに保存する前に画像を一時的に保存することでした。ここ
はコードSystem.IO.IOException:プロセスがファイルにアクセスできない

private void btnSave_Click(object sender, EventArgs e) 
    { 
     imgTemp = new System.Windows.Forms.PictureBox(); 
     imgTemp.Image = Image.FromFile(@cwd + "\\Final.jpg"); 
     MemoryStream mstr = new MemoryStream(); 
     imgTemp.Image.Save(mstr, imgTemp.Image.RawFormat); 
     byte[] arrImage = mstr.GetBuffer(); 
     //Set insert query 
     imgTemp.Image = null; 
     imgTemp.Dispose(); 

     string qry = "insert into FinalImages (FinalImageName, FinalImage, Parts) values(@FinalImageName, @FinalImage, @Parts)"; 

     SqlConnection c = new SqlConnection(c_string); 
     //Initialize SqlCommand object for insert. 
     SqlCommand SqlCom = new SqlCommand(qry, c); 

     //We are passing Original Image Path and Image byte data as sql parameters. 
     SqlCom.Parameters.Add(new SqlParameter("@FinalImageName", SqlDbType.Char, 40)).Value = textBox1.Text + ".jpg"; 
     SqlCom.Parameters.Add(new SqlParameter("@FinalImage", SqlDbType.Image)).Value = arrImage; 
     SqlCom.Parameters.Add(new SqlParameter("@Parts", SqlDbType.VarChar, 40)).Value = NumOfFiles; 

     try 
     { 
      c.Open(); 
      SqlCom.ExecuteNonQuery(); 
     } 
     catch (System.Data.SqlClient.SqlException err) 
     { 
      MessageBox.Show(err.Message); 
     } 
     finally 
     { 
      c.Close(); 
     } 

     // How many Picture files in this folder 
     imgArray2 = new System.Windows.Forms.PictureBox[NumOfFiles]; 
     for (int i = 0; i < NumOfFiles; i++) 
     { 
Bitmap(imgName[i]); 
      imgArray2[i] = new System.Windows.Forms.PictureBox(); 
      imgArray2[i].Image = Image.FromFile(imgName[i]); 
      string name2 = textBox1.Text + ".jpg"; 
      string name3 = imgName[i].Substring(imgName[i].LastIndexOf(@"\") + 1,   imgName[i].Length - imgName[i].LastIndexOf(@"\") - 1); 
      MemoryStream mstr2 = new MemoryStream(); 
      imgArray2[i].Image.Save(mstr2, imgArray2[i].Image.RawFormat); 
      byte[] arrImage2 = mstr2.GetBuffer(); 
      string cmd2 = "insert into ImageParts (FinalImageName, ImagePartName, ImagePart) values (@FIName2, @IPName, @IP)"; 

      SqlConnection c2 = new SqlConnection(c_string); 
      SqlCommand comm2 = new SqlCommand(cmd2, c2); 
      comm2.Parameters.Add(new SqlParameter("@FIName2", SqlDbType.Char, 40)).Value = name2; 
      comm2.Parameters.Add(new SqlParameter("@IPName", SqlDbType.Char, 40)).Value = name3; 
      comm2.Parameters.Add(new SqlParameter("@IP", SqlDbType.Image)).Value = arrImage2; 

      try 
      { 
       c2.Open(); 
       comm2.ExecuteNonQuery(); 
      } 
      catch (System.Data.SqlClient.SqlException err) 
      { 
       MessageBox.Show(err.Message); 
      } 
      finally 
      { 
       c2.Close(); 
      } 

     } 
     DelDir(); 
     this.Hide(); 
     fourthForm.Show(); 
    } 

    private void DelDir() 
    { 
     string[] files = Directory.GetFiles(cwd); 
     string[] dirs = Directory.GetDirectories(cwd); 

     foreach (string file in files) 
     { 
      File.SetAttributes(cwd, FileAttributes.Normal); 
      File.Delete(file); 
     } 

     Directory.Delete(cwd, false); 
    } 

であり、これは完全な例外

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll 
System.IO.IOException: The process cannot access the file 'C:\...\Final.jpg' because it is being used by another process. 
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
at System.IO.File.Delete(String path) 
at BlueStitch.frmStitch.DelDir() in C:\...\frmStitch.cs:line 953 
at BlueStitch.frmStitch.button1_Click(Object sender, EventArgs e) in C:\...\frmStitch.cs:line 940 
at System.Windows.Forms.Control.OnClick(EventArgs e) 
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
at System.Windows.Forms.Control.WndProc(Message& m) 
at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
at System.Windows.Forms.Button.WndProc(Message& m) 
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
at BlueStitch.Program.Main() in C:\Users\Freddie Rosillo\Documents\Visual Studio 2008\Projects\BlueStitch\BlueStitch\BlueStitch\Program.cs:line 21 

ラインは私がイメージファイルを配置しようとしたと思いますが、それでも
助けを動作しませんFile.Delete(file);
たですしてください

答えて

0

私はこれに肯定的ではありませんが、私はあなたが非常に最初の一部:

imgTemp.Image = null; 
    imgTemp.Dispose(); 

しかし、あなたは第二部でそれを行うことを怠ってきた:

 imgArray2[i].Image = Image.FromFile(imgName[i]); 
     string name2 = textBox1.Text + ".jpg"; 
     string name3 = imgName[i].Substring(imgName[i].LastIndexOf(@"\") + 1,   imgName[i].Length - imgName[i].LastIndexOf(@"\") - 1); 
     MemoryStream mstr2 = new MemoryStream(); 
     imgArray2[i].Image.Save(mstr2, imgArray2[i].Image.RawFormat); 
     byte[] arrImage2 = mstr2.GetBuffer(); 

この最後の部分にラインにこれ​​らを追加してみてください:これら二つの

//Set insert query 
    imgArray2[i].Image = null; 
    imgArray2[i].Dispose(); 
2

ルック行:

imgTemp.Image = null; 
imgTemp.Dispose(); 

PictureBoxを処分する前に画像に戻ってください。つまり、PictureBoxは、Dispose()メソッドを呼び出すと画像を破棄できません。ガベージコレクタがイメージのファイナライザを呼び出すまでイメージは破棄されません。

関連する問題