I have a simple form which the users can use to upload images. It has a preview button so when User selects an image and clicks on preview, a postback occurs and I save the imag
Why are you using a Bitmap to open the file on the server side? You should simply stream the "file" using
Response.TransmitFile(physicalFileNameAndPath).
Where TransmitFile expects (as a string) the physical file path and name.
The problem stems from the the way you send the file to the client. The file is still "locked" by the previous step. If you fix this, you'll be able to delete the file if needed.
I've not used the FileUpload control but it looks like you're using ViewState (which is what helps retain the values in the fields after postback. Either the FileUpload control does not support ViewState or you've not configured it to use ViewState.
you could assign the required property of the FileUpload control with the correct "value" so it shows what you've expect. This would mean that you'd have to capture the said value when the form is posted and simply re-assign the value back to the FileUpload control's property during the DataBind.
The exception talks for itself : Your file is still being used/opened by another process, i.e. thread, that you have launched. I bet it is the process by which you open the image file for reading. Make sure you have closed the relevant stream, ten bucks that it will solve your issue.