locked
Protective/defensive coding while manipulating image RRS feed

  • Question

  • User-186437670 posted
    <color="#6633cc">Edited by SomeNewKid. Please post code between </color><color="#000000"><</color>code><color="#6633cc"> and </color><color="#000000"><</color>/code><color="#6633cc"> tags.</color>
    Hey there, I'm a bit of a hack and was wondering the best way to use protective or defensive coding when I am manipulating an image and save to a file. It became obvious while debugging that I was going to need something like this because when an error occurred, my file was last open and then couldn't be deleted, overwritten, etc. I know I need to use the Try ... Catch strucutre but I'm not sure how to implement it in this case. Can someone help me out? Here is some sample code: Sub btnSubmit_Click(sender As Object, e As EventArgs) Dim InputBitmap as Bitmap = new Bitmap(Server.MapPath(sSavePath + "temp.jpg")) Dim newWidth as integer = 100 Dim newHeight as integer = 125 Dim OutputBitmap as Bitmap = new Bitmap(InputBitmap, newWidth, newHeight) InputBitmap.Dispose() Response.Clear() Response.ContentType="image/jpeg" OutputBitmap.Save(Server.MapPath(sSavePath + sFilename)) OutputBitmap.Dispose() 'Delete temp file(?) End Sub
    Tuesday, August 17, 2004 1:06 AM

All replies

  • User-599719271 posted
    enclose anything which can thrown an exception with a try block, catch the exception in the catch block, and do your cleaning up and releasing in the finally block. it's all in the docs.
    Tuesday, August 17, 2004 4:15 AM
  • User-186437670 posted
    Atrax wrote: > ... it's all in the docs. That's a fairly standard response from you, but not particularly helpful. Yes, I've read the docs and I know how to use Try ... Catch, but what I don't understand if how to prevent an open file from remaining open in the case of any error. Can I put a delete command in the Catch? Will this help?
    Tuesday, August 17, 2004 6:22 PM
  • User-599719271 posted
    you didn't read it did you? do your cleanup in the finally block. if anything goes wrong, you can release resources and locks there. Please pay attention.
    Wednesday, August 18, 2004 12:16 AM
  • User-186437670 posted
    I have read several sources of documentation extensively and I did pay attention. You have, however, ruined my experience in using this forum to gain a deeper and more complete understanding of the problem that I was trying to resolve. Your obnoxious attitude is not at all appreciated and I hope it reaches a moderator's attention. No thanks.
    Wednesday, August 18, 2004 4:46 PM
  • User-1372641848 posted
    Atrax said :: enclose anything which can thrown an exception with a try block, catch the exception in the catch block, and do your cleaning up and releasing in the finally block Which translates to : try{ do stuff... }catch(Exception ex) { do some thing if exception occours. }finally { do your cleaning in this block this gets called no matter what. } Hope this clarifies..
    Wednesday, August 18, 2004 11:04 PM