Unable to delete file

I am having a problem deleting the original file after I convert it. The process I have is as follows: The user uploads a file and it is saved to the file system, then I use Aspose to convert the file to PDF and save it to the file system. Then I upload the new pdf to the SQL FileStream and delete the pdf and the original file. The problem is that there is something locking the original file where I cannot perform a File.Delete() on it. I get the following exception when I wrap a Try/Catch around the File.Delete():

File delete error. System.IO.IOException: The process cannot access the file ‘C:\Websites\V8.operationhomefront.net\ClientUploadedFiles\ffc708e7-a0ed-4e4b-8dd5-319088d6e012.jpg’ because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at MafaCase.UploadFiles_Click(Object sender, EventArgs e) in c:\Websites\V8.operationhomefront.net\MafaCase.aspx.cs:line 1126

I am attaching the code (striped of non-essential code).

Edit: I even added a pdf.Close() after the pdf.Save() method.

Hello Mike,

Thanks for using our products.

I have gone through the code snippet that you have shared and as per my understanding, the problelm is occurring because in following code line System.Drawing.Image srcImage = System.Drawing.Image.FromFile(InputFile); , you have create Image object and later on you have passed it ResizeImage(...) method to create a new dstImage Image object, and without releasing the resource being used by System.Drawing.Image object, you are trying to delete the source file. In order to delete the original source file, please try using srcImage.Dispose(); before calling File.Delete(InputFile);. Please try using it and in case you still face any problem or you have any further query, please feel free to contact. We are sorry for this inconvenience.

Besides this, I am not entirely certain why you are using System.Drawing.Image to create a resized image object, when you can simply resize the image while using Image.ImageInfo.FixHeight and Image.ImageInfo.FixWidth properties.

Thanks, that did the trick. As for the using the System.Drawing.Image to resize I could not find any examples as how to resize if I do not know the size of the image. Your documentation is vague and provides no examples. Can I use image.ImageScale to make the over-sized image fit within the pdf? Can you provide an example of how it is used? Do I set it like this:

image.ImageScale = 100d;

Thanks,
Mike

Hello Mike,

I am glad to hear that your problem is resolved.

The ImageScale property of Image class is used to set the scale rate of the image when placed into pdf file (value should be float). More along, you can also use FixHeight and FixWidth properties of ImageInfo class in case you need to specify the Height and Width information for the image file before placing it into PDF document. Please visit the following links for more information on

In case you still face any problem or you have any further query, please feel free to contact. We apologize for your inconvenience.