Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

My website
Posted

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...

Posted

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)

My website
Posted

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

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

Posted
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.

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

Posted

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!

My website
Posted

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

My website
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...