Pic resize RRS feed

  • Question

  • User-239001081 posted

    Hi there,

    I have a form which I use for uploading pics using the AsyncFileUpload that works as expected. After the AsyncFileUpload.SaveAs ends I am trying to resize the uploaded pic with the following code -

    Protected Sub FileUploadComplete(ByVal sender As ObjectByVal e As EventArgs)
            Dim filename As String = System.IO.Path.GetFileName(AsyncFileUpload1.FileName)
            AsyncFileUpload1.SaveAs(Server.MapPath(UploadFolderPath) + filename)
            Dim originalBM As New Bitmap(Server.MapPath(UploadFolderPath) + filename)
            Dim newheight As Integer = 300 * originalBM.Height / originalBM.Width
            Dim newSize As New Size(300, newheight)
            Dim newBM As New Bitmap(originalBM, newSize) 
            Dim newFileName As String = Server.MapPath(UploadFolderPath) + "new." + filename
            newBM.Save(newFileName, originalBM.RawFormat)
            replaceFiles(Server.MapPath(UploadFolderPath) + filename, newFileName)
        End Sub
        Private Function replaceFiles(org As String, newImg As StringAs Boolean
            If File.Exists(org) And File.Exists(newImg) Then
                Dim fi As New FileInfo(org)
                    fi.Replace(newImg, newImg + ".bak")
                    Return True
                Catch ex As Exception
                    Return False
                End Try
                Return False
            End If
        End Function

    When the code runs I get the following error -

    The process cannot access the file because it is being used by another process.

    How can I avoid this error ?

    Monday, November 21, 2011 8:30 AM


  • User1958474683 posted


    you need to disppose the bitmap object to realese all its reasources aftre saving it.



    Good Luck
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 21, 2011 9:08 AM