I am trying to write an image viewer that will show JPEG files. I can get them to show using an Image control with no trouble.

I have a button on the form that I want to use to rotate the displayed image by 90 degrees, save the changes to disk and then reload the image in the Image control.

I am using the following code:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        Dim extension As String = Path.GetExtension("c:\jpg.jpg")
        Dim rootFilename As String = Path.GetDirectoryName("c:\jpg.jpg") & "\" & _
            Path.GetFileNameWithoutExtension("c:\jpg.jpg")
        Dim dtstart As DateTime = Now
        Dim imageSource As BitmapSource = Image1.Source
        imageSource = New TransformedBitmap(imageSource, New RotateTransform(90))
        Dim encoder As New JpegBitmapEncoder
        encoder.QualityLevel = 80
        encoder.Frames.Add(BitmapFrame.Create(imageSource))
        imageSource = Nothing
        Dim fs As New IO.FileStream(rootFilename & extension & "-bak", FileMode.Create, FileAccess.Write)
        encoder.Save(fs)
        fs.Dispose()
        Dim dtend As DateTime = DateTime.Now
        Dim sw As New IO.StreamWriter("c:\testing\results.txt", True)
        sw.WriteLine("WPF Start at: " & dtstart.ToLongTimeString)
        sw.WriteLine("WPD End at: " & dtend.ToLongTimeString)
        sw.Dispose()
        encoder = Nothing
        Image1.Source = Nothing
        IO.File.Delete(rootFilename & extension)
        IO.File.Move(rootFilename & extension & "-bak", rootFilename & extension)
        Image1.Source = New BitmapImage(New Uri(rootFilename & extension))

    End Sub
At the line "IO.File.Delete(rootFileName & extension)", I keep getting an error that "c:\jpg.jpg" cannot be accessed because it is being used by another process. I have pored over the code and I think I've disposed of all of the resources that relate to "c:\jpg.jpg", right? What am I missing? I'm tired and may be missing something really easy, but I'm new to WPF, and some of it's confusing to me, so please be gentle.