File in use error while renaming file
-
Dear Group, Here's what my app is attempting: 1) rename a JPEG file temporarily 2) create a thumbnail for that JPEG 3) save the thumbnail in a separate folder 4) rename the JPEG file back to its original name 1), 2), and 3) work fine. 4) is where I have the problem. My program halts with this error: "The process cannot access the file because it is being used by another process." Here's the code:
' Step 1)
My.Computer.FileSystem.RenameFile("TestImage.jpg, _
"TestImage.jpg.working")
' Step 2)
Dim bmpSS As Bitmap
Dim intBW As Integer ' thumbnail width
Dim intBH As Integer ' thumbnail height
Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")
bmpSS = New Bitmap(250, CInt(bmp.Height / (bmp.Width / 250)), _
Imaging.PixelFormat.Format32bppArgb)
intBW = bmpSS.Width
intBH = bmpSS.Height
Dim g As Graphics = Graphics.FromImage(bmpSS)
g.DrawImage(Bmp, 0, 0, intBW, intBH)
' Step 3)
bmpSS.Save("C:\Temp\TestImage_250.jpg")
g = Nothing
bmp = Nothing
bmpSS = Nothing
' Step 4)
My.Computer.FileSystem.RenameFile("TestImage.jpg.working", _
"TestImage.jpg")As I said, it all works until it hits step 4). Is there some locking issue that I'm missing here? Regards, Steve Erbach Neenah, WI http://TheTownCrank.blogspot.com
-
Dear Group, Here's what my app is attempting: 1) rename a JPEG file temporarily 2) create a thumbnail for that JPEG 3) save the thumbnail in a separate folder 4) rename the JPEG file back to its original name 1), 2), and 3) work fine. 4) is where I have the problem. My program halts with this error: "The process cannot access the file because it is being used by another process." Here's the code:
' Step 1)
My.Computer.FileSystem.RenameFile("TestImage.jpg, _
"TestImage.jpg.working")
' Step 2)
Dim bmpSS As Bitmap
Dim intBW As Integer ' thumbnail width
Dim intBH As Integer ' thumbnail height
Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")
bmpSS = New Bitmap(250, CInt(bmp.Height / (bmp.Width / 250)), _
Imaging.PixelFormat.Format32bppArgb)
intBW = bmpSS.Width
intBH = bmpSS.Height
Dim g As Graphics = Graphics.FromImage(bmpSS)
g.DrawImage(Bmp, 0, 0, intBW, intBH)
' Step 3)
bmpSS.Save("C:\Temp\TestImage_250.jpg")
g = Nothing
bmp = Nothing
bmpSS = Nothing
' Step 4)
My.Computer.FileSystem.RenameFile("TestImage.jpg.working", _
"TestImage.jpg")As I said, it all works until it hits step 4). Is there some locking issue that I'm missing here? Regards, Steve Erbach Neenah, WI http://TheTownCrank.blogspot.com
serbach wrote:
Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")
You need to call Dispose on this object before you'll be able to rename or overwrite the file in question.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
serbach wrote:
Dim bmp As Bitmap = New Bitmap("TestImage.jpg.working")
You need to call Dispose on this object before you'll be able to rename or overwrite the file in question.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian, » You need to call Dispose on this object before you'll be able to rename or overwrite the file in question. « Thank you! That is precisely what I needed! Sincerely, Steve Erbach Neenah, WI http://TheTownCrank.blogspot.com