views:

383

answers:

2

Hi

I'm writing a little program where I select a picture through an open file dialogue. When I selected a picture I want it to overwrite the current picture and display the new image. Now I don't have any problems with picking an image with a different extension. So when I currently have a .png I can select a .jpg but when I choose an image with the same extension as the current image I get an error: The process cannot access the file 'C:\Users....\woontypeimages\chalet_foto.jpg' because it is being used by another process.

    If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Dim sFilename As String = cboWoningtypesWoningtype.SelectedItem.ToString & "_foto" & System.IO.Path.GetExtension(ofd.FileName)
        System.IO.File.Copy(ofd.FileName, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & sFilename, True)
        txtWoningtypesFoto.Text = sFilename
        updateImages()
    End If

    Private Sub updateImages()

    Try
        picFoto.Image = Nothing
        txtWoningtypesFoto.BackColor = clrReadonly
        txtWoningtypesFoto.ForeColor = Color.Black
        picFoto.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesFoto.Text)
    Catch ex As Exception
        txtWoningtypesFoto.BackColor = clrError
        txtWoningtypesFoto.ForeColor = Color.White
    End Try
    Try
        picGrondplan.Image = Nothing
        txtWoningtypesGrondplan.BackColor = clrReadonly
        txtWoningtypesGrondplan.ForeColor = Color.Black
        picGrondplan.Image = System.Drawing.Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Camping Relaxx\woontypeimages\" & txtWoningtypesGrondplan.Text)
    Catch ex As Exception
        txtWoningtypesGrondplan.BackColor = clrError
        txtWoningtypesGrondplan.ForeColor = Color.White
    End Try

End Sub

If anyone could help me I would be pleased

Thanks in advance

+2  A: 

Use these :

picFoto.Image.Dispose()
picGrondplan.Image.Dispose()

instead of :

picFoto.Image = Nothing
picGrondplan.Image = Nothing

The Image.FromFile method maintains a lock on the source file until the image has been disposed. Setting an object to nothing does not immediately dispose it - the garbage collector will take care of that in its own time (which might well not be until you've closed the form with the picture box on). Dispose is required to immediately free the file handle.

CodeByMoonlight
I'd also recommend a using statement, so that the dispose doesn't need to be as explicit.
Mike L
@Mike L, this code looks like a subroutine that just sets the image so a using statement wouldn't work because it would set the image and destroy it right away.
Chris Haas
+1  A: 

Instead of worrying about Dispose() you can instead use the Load(string) method of the PictureBox which won't lock the file.

Me.PictureBox1.Load("C:\test.png")
Chris Haas