Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting an error
System.IO.IOException: 'The process cannot access the file 'C:\Users\james\Desktop\chem\d70e1e925d85464e259cb8a7488aec78.png' because it is being used by another process.'


I am trying to delete a picture after uploading it to my database.

Here is my code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       Dim Files As New OpenFileDialog
       Files.Filter = "Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"
       ' Allow the user to select multiple images.
       Files.Multiselect = True
       Files.Title = "Select an image"
       Files.ShowDialog()
       PictureBox1.Image = Image.FromFile(Files.FileName)

       Try
           Dim sqlcon As New SqlConnection("constring")
           Dim sqladapt = New SqlDataAdapter("Select * from [Table]", sqlcon)

           sqlcon.Open()
           Dim cmd As SqlClient.SqlCommand
           Dim sql As String = "insert into [Table] values(@ID,@Question,@Answers,@Howtowork,@Chapter,@img)"
           cmd = New SqlClient.SqlCommand(sql, sqlcon)

           Using ms As MemoryStream = New MemoryStream()

               Dim bm As Bitmap = New Bitmap(PictureBox1.Image)
               bm.Save(ms, PictureBox1.Image.RawFormat)

               Dim arrPic() As Byte = ms.GetBuffer()

               ' Make sure that all tables have the same type of information that can be entered, if not you will recieve an error.
               cmd.Parameters.AddWithValue("@ID", DataGridView1.Rows.Count)
               cmd.Parameters.AddWithValue("@Question", TextBox2.Text)
               cmd.Parameters.AddWithValue("@Answers", TextBox3.Text)
               cmd.Parameters.AddWithValue("@Howtowork", TextBox4.Text)
               cmd.Parameters.AddWithValue("@Chapter", ComboBox2.Text)
               cmd.Parameters.AddWithValue("@img", arrPic)
               cmd.ExecuteNonQuery()

               ms.Close()

           End Using

           sqlcon.Close()

           MessageBox.Show("New Record Added")

       Catch ex As Exception

       End Try

       TextBox2.Clear()
       TextBox3.Clear()
       TextBox4.Clear()

       refreshtable()

       System.IO.File.Delete(Files.FileName)


   End Sub


What I have tried:

I have tried an ms.dispose, bm.dispose and a picturebox1.dispose. I figured that one of those would disconnect from the file before so I could delete. However, I am still getting the same error
Posted
Updated 27-Mar-18 1:41am

1 solution

This limitation has been known for many years:
Image file is locked when you set the PictureBox Image property to a file[^]

Replace:
VB.NET
PictureBox1.Image = Image.FromFile(Files.FileName)
With:
VB.NET
Using fs As New IO.Stream(Files.FileName, IO.FileMode.Open, IO.FileAccess.Read)
    PictureBox1.Image = Image.FromStream(fs)
End Using
 
Share this answer
 
v2
Comments
Member 11856456 27-Mar-18 10:31am    
Thanks, I could not figure out why I could not just dispose of the information and then remove the image file afterward. So, Never use the Image.fromfile() code, focus on using a stream instead?
Richard Deeming 27-Mar-18 10:32am    
Yes, you should generally avoid Image.FromFile, since it locks the file until your application closes.

As is so often the case with Microsoft, this bug is "by design". :)
Member 11856456 27-Mar-18 11:41am    
Thanks, Richard, I will keep a mental note of this. Thanks again for the help.
turbozob 26-Mar-19 4:36am    
Works. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900