Question

I am working on windowes form application..in show button event i wrote code like this:

Me.PictureBox1.Load("C:/Signature.tif")
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Me.PictureBox1.BorderStyle = BorderStyle.Fixed3D

then save button click i wrote code like this:

Dim exittime As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Dim ms As New MemoryStream
Dim byt() As Byte
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
byt = ms.GetBuffer
Dim sqlstr As String = "Update Visitorlogo_tbl set signimage=@pic,exittime='" & exittime & "',status=2 where PassNo='" & txtvisitorid.Text & "'"
Dim cmd1 As New SqlCommand(sqlstr, con.connect)
cmd1.Parameters.Add("@pic", SqlDbType.Image)
cmd1.Parameters("@pic").Value = byt
cmd1.ExecuteNonQuery()
con.disconnect()
PictureBox1.Image = Nothing
If System.IO.File.Exists("C:/Signature.tif") Then
   System.IO.File.Delete("C:/Signature.tif")
End If

while saving image image got saving,,but after that i want to delete image from that path.. while coming to this line : System.IO.File.Delete("C:/Signature.tif") am getting error: The process cannot access the file 'C:\Signature.tif' because it is being used by another process

Was it helpful?

Solution

Possible the problem is here.

Me.PictureBox1.Load("C:/Signature.tif")

try this

Me.PictureBox1.Image = new Bitmap("C:\Signature.tif");

UPDATED:

PictureBox1.Load() method will load the file from given location and stores the file path in PictureBox.ImageLocation property. with this method, application will open that image and lock so, other user cannot modify or read it.

PictureBox1.Image = new Bitmap("filePath"); will create the another image object from given file path and it will not lock down the original one. This method will not load the original image from given file location. So, the PictureBox1.ImageLocation property will not be set here. How you can access that image and modify it.

OTHER TIPS

The PictureBox will keep the file open. Therefore you can use the fix suggested by @Shell to release the file after reading the contents. This behavior is by design of the PictureBox.

http://support.microsoft.com/kb/309482

Here is a workaround adapted from the knowledge base article

Using fs as New System.IO.FileStream("C:\Signature.tif", IO.FileMode.Open, IO.FileAccess.Read)
    PictureBox1.Image = System.Drawing.Image.FromStream(fs)
End Using

As the error message suggests the image is open by another process. Have you got the image open in a graphics program for example?

Do you have another instance of your application running in the background that has locked the image for reading?

If so close the other programs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top