hog Posted July 25, 2005 Posted July 25, 2005 I am using the following to delete a file which is being displayed in a picturebox. File.Delete(strFileName) I keep getting an error message saying the file is in use by another process and cannot be deleted? I have set the picturebox to nothing, I have set the bitmap used to fill the picturebox to nothing and even set the FileInfo() object which holds the file name to nothing and still get the error???? Where am I going wrong? Thnx Quote My website
Shurikn Posted July 25, 2005 Posted July 25, 2005 if the file a picture(since you said that you are displaying it in a picture box) and if so(stupid question) is it open by another app or in a share maybe? or... are you using winXp? I know it really have problem letting go of past used things... theye should be a way to destroy any link the OS have with any files... but M$ dont want that... I wouldnt care killing windows a couple of time if I could kill every process and delete any file I want when I want... Quote
Administrators PlausiblyDamp Posted July 25, 2005 Administrators Posted July 25, 2005 You may want to try disposing of the bitmap object rather than just setting it to nothing. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hog Posted July 25, 2005 Author Posted July 25, 2005 Yeah I am running WIndozeXP :-\ I have tried this so far and still no joy, and my program IS the only program opening the bitmap to view? m_bmpNewImage.Dispose() fiFileInfo = Nothing Me.picMain.Image = Nothing File.Delete(strFileName) Quote My website
SonicBoomAu Posted July 25, 2005 Posted July 25, 2005 A reference to that file might still be in memory. You could force a Garbage collection and see if that removes the file fomr memory. gc.collect Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
hog Posted July 25, 2005 Author Posted July 25, 2005 So KOOL :D gc.collect fixed the problem :D :D Thnx :D :D Quote My website
Administrators PlausiblyDamp Posted July 25, 2005 Administrators Posted July 25, 2005 Did you try calling dispose on the bitmap like I suggested. Calling GC.collect is rarely the correct solution to the problem. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
SonicBoomAu Posted July 25, 2005 Posted July 25, 2005 Yeah I am running WIndozeXP :-\ I have tried this so far and still no joy, and my program IS the only program opening the bitmap to view? m_bmpNewImage.Dispose() fiFileInfo = Nothing Me.picMain.Image = Nothing File.Delete(strFileName) No offence PlausiblyDamp but it seems to me hog tried to dispose of the bitmap as you sugested, but with no joy. If it wasn't for this I wouldn't has suggested the garbage collection. I realise that windows does it own garbage collection in it own time but sometimes you have to give it a push. Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
thenerd Posted July 26, 2005 Posted July 26, 2005 No, he didn't dispose it. He set it to nothing. They're different, he should have called me.picmain.image.dispose() Quote
Administrators PlausiblyDamp Posted July 26, 2005 Administrators Posted July 26, 2005 If not the image then the FileStream used to load the image may need disposing, although without seeing the loading code that could be a bit of guesswork. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hog Posted July 26, 2005 Author Posted July 26, 2005 This is all the code that has anything to do with the file. bmpResized = New Bitmap(CInt(bmpTooBig.Width * decRatio), CInt(bmpTooBig.Height * decRatio)) grpBitmap = Graphics.FromImage(bmpResized) fiFileInfo = diDirectory.GetFiles("*.jpg") m_bmpNewImage = ResizeBitmap(New Bitmap(strFileName), intPictureBoxWidth, intPictureBoxHeight) me.picMain.image = m_bmpNewImage I have tried setting all to nothing and disposing and nothing works except the gc.collect! Quote My website
Administrators PlausiblyDamp Posted July 26, 2005 Administrators Posted July 26, 2005 Could you attach the project in question - might be easier to track down what needs disposing if all the code is available. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hog Posted July 27, 2005 Author Posted July 27, 2005 Here are the sections of code that have anything to do with the file. Private Function ResizeBitmap(ByVal bmpTooBig As Bitmap, ByVal maxWidth As Integer, ByVal maxHeight As Integer) As Bitmap Dim decRatio, decRatioW, decRatioH As Decimal Dim bmpResized As Bitmap Dim grpBitmap As Graphics If bmpTooBig.Width > maxWidth Then decRatioW = maxWidth / bmpTooBig.Width Else decRatioW = 1 End If If bmpTooBig.Height > maxHeight Then decRatioH = maxHeight / bmpTooBig.Height Else decRatioH = 1 End If 'determine which dimension is the smallest If decRatioW <= decRatioH Then decRatio = decRatioW Else decRatio = decRatioH End If bmpResized = New Bitmap(CInt(bmpTooBig.Width * decRatio), CInt(bmpTooBig.Height * decRatio)) 'Make a Graphics object for the resized bitmap. grpBitmap = Graphics.FromImage(bmpResized) grpBitmap.DrawImage(bmpTooBig, 0, 0, bmpResized.Width + 1, bmpResized.Height + 1) If Me.chkFullScreen.Checked Then Me.picMain.Location = New Point((intSavePictureBoxWidth - bmpResized.Width) / 2, 0) Else Me.picMain.Location = New Point((intSavePictureBoxWidth - bmpResized.Width) / 2, 104) End If Me.picMain.Width = bmpResized.Width Return bmpResized 'dispose of graphic objects grpBitmap.Dispose() End Function Private Sub LoadSites() Try ' start up path to find photos Dim strPath As String = gstrPhotosFolderPath & Me.cboSites.SelectedItem & "\" & Me.cboDates.SelectedItem Dim diDirectory As New DirectoryInfo(strPath) Dim fiTemp As FileInfo ' create an array of the files in the current directory fiFileInfo = diDirectory.GetFiles("*.jpg") intPhotoCount = fiFileInfo.Length intIndex = 0 ' store the first photo path strFileName = gstrPhotosFolderPath & Me.cboSites.SelectedItem & "\" & Me.cboDates.SelectedItem & "\" & fiFileInfo(intIndex).ToString ' load the image m_bmpNewImage = ResizeBitmap(New Bitmap(strFileName), intPictureBoxWidth, intPictureBoxHeight) ' display it Me.picMain.Image = m_bmpNewImage Catch objException As Exception MessageBox.Show(objException.Message) End Try End Sub Quote My website
Administrators PlausiblyDamp Posted August 1, 2005 Administrators Posted August 1, 2005 in the line m_bmpNewImage = ResizeBitmap(New Bitmap(strFileName), intPictureBoxWidth, intPictureBoxHeight) the New Bitmap(strFileName) part is probably locking the file. Try creating a temp bitmap, passing that in and disposing of the temp one to see if the problem goes away. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.