0

I am converting an TIFF file to a .PNG file in vb.net. First, I save the TIFF file to a specific location (given by NewFileName). Then I convert the file to .PNG using the Bitmap.Save method. Later in my program, I attempt to delete all the .tif files. However, I get an error that says the files are still in use. I have done some research about the reasons for this and I have read that a lot of the errors come from not closing filestream. However, I do not use filestream in my program so I think it is something else. Another possibility that was suggested was that the file was opened twice. I have scoured my code and I am pretty sure the files were never opened, only saved and accessed with the bitmap.Save command. I also downloaded handle.exe and process explorer to find which process was locking the files. Apparently, the files are only in use by the program once I convert them to PNG using the bitmap.save command. Maybe there is a way to close bitmap.Save? Any other suggestions of what to add would be appreciated as well.

    objApplication.StartCommand(SolidEdgeConstants.AssemblyCommandConstants.AssemblyViewBottomView)
    view = window.View
    view.Fit()
    withoutExt = "C:\Folder" & "\" & shortForms(12) & FileName
    NewFileName = withoutExt & ".tif"
    view.SaveAsImage(NewFileName, Width:=width, Height:=height, Resolution:=resolution, ColorDepth:=colorDepth)
    System.Drawing.Bitmap.FromFile(NewFileName).Save(withoutExt & ".png", System.Drawing.Imaging.ImageFormat.Png)

Thanks in advance!

Aidan Kehoe
  • 104
  • 3
  • 14
  • Can you share the code where you first open the TIFF ? – Robin Mackenzie May 24 '16 at 09:29
  • @robin I'm sorry my question was misleading. I never directly open the TIFF, I simply access it with the .saveasimage command and the bitmap.save command. I will edit the question. – Aidan Kehoe May 24 '16 at 09:34
  • @robin I edited the post and included that I found the files were being locked by the bitmap.save command. Maybe that helps. – Aidan Kehoe May 24 '16 at 09:38
  • 1
    The 'FromFile' method is placing the lock - longish discussion here about your issue [link](http://stackoverflow.com/questions/18250848/how-to-prevent-the-image-fromfile-method-to-lock-the-file). Maybe you need to do the FromFile and then Save in two steps preserving the FromFile return in a variable which you can dispose before deleting the TIFF. – Robin Mackenzie May 24 '16 at 09:46
  • I tried separating into two steps. NewFileName was still locked because I need to use it as the path for converting the TIFF file to PNG. – Aidan Kehoe May 24 '16 at 09:58
  • Maybe it's the `view` object that needs to be disposed before you try and delete the TIFF? – Robin Mackenzie May 24 '16 at 10:01
  • @robin Sorry for the late reply. I tried to dispose view but I don't think you can dispose a solidedeframework object until you close the application. I tried to convert to streamwriter to unlock the file, but since I am converting between file types I need to use the bitmap.save method. – Aidan Kehoe May 24 '16 at 12:19

1 Answers1

0

I figured it out. All you need is a using statement so that NewFileName is released before I delete it. I changed the code to this and it worked:

    view = window.View
    view.Fit()
    withoutExt = ChosenFile & "\" & shortForms(13) & FileName
    NewFileName = withoutExt & ".tif"
    view.SaveAsImage(NewFileName, Width:=width, Height:=height, Resolution:=resolution, ColorDepth:=colorDepth)
    Using tempImage = System.Drawing.Bitmap.FromFile(NewFileName)
        tempImage.Save(withoutExt & ".png", System.Drawing.Imaging.ImageFormat.Png)
    End Using
    My.Computer.FileSystem.DeleteFile(NewFileName)

Thanks everyone for your help!

Aidan Kehoe
  • 104
  • 3
  • 14