Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Small tip on using images
Message
From
10/04/2016 14:56:23
 
 
To
10/04/2016 11:53:52
General information
Forum:
ASP.NET
Category:
Pictures and Image processing
Environment versions
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01634620
Message ID:
01634626
Views:
43
>I would like to share a small tip on using images.
>
>I had a method like this:
>
>
>    ' Crop an image
>    Public Function Crop() As Boolean
>        Dim loGraphics As System.Drawing.Graphics = Nothing
>        Dim loImage As System.Drawing.Bitmap = Nothing
>
>        ' This is a placeholder
>        loImage = New System.Drawing.Bitmap(nCropWidth, nCropHeight)
>
>	loGraphics = System.Drawing.Graphics.FromImage(loImage)
>
>        ' This will use oImage and obtain the new result into loImage
>        loGraphics.DrawImage(oImage, New System.Drawing.Rectangle(0, 0, nCropWidth, nCropHeight), _
>         nStartXPoint, nStartYPoint, nCropWidth, nCropHeight, System.Drawing.GraphicsUnit.Pixel)
>
>	' Release this object
>	loGraphics.Dispose()
>
>        ' Take the new result and dump it back to oImage
>        oImage = loImage.Clone
>
>	' Release this object
>	loImage.Dispose()
>
>	Return True
>End Function
>
>
>On a very particular situation, I ended up with a file lock issue. I have been troubleshooting this for a while and only found the reason today.
>
>When you look at the oImage assignation at the end, many would see it as ok. And, we could think like that even if we already had a reference of a file in oImage, a System.Drawing.Bitmap object.
>
>But, in an isolated situation, it turned out this small dispose before assigning the new reference was needed:
>
>
>    ' Crop an image
>    Public Function Crop() As Boolean
>        Dim loGraphics As System.Drawing.Graphics = Nothing
>        Dim loImage As System.Drawing.Bitmap = Nothing
>
>        ' This is a placeholder
>        loImage = New System.Drawing.Bitmap(nCropWidth, nCropHeight)
>
>	loGraphics = System.Drawing.Graphics.FromImage(loImage)
>
>        ' This will use oImage and obtain the new result into loImage
>        loGraphics.DrawImage(oImage, New System.Drawing.Rectangle(0, 0, nCropWidth, nCropHeight), _
>         nStartXPoint, nStartYPoint, nCropWidth, nCropHeight, System.Drawing.GraphicsUnit.Pixel)
>
>	' Release this object
>	loGraphics.Dispose()
>
>	' Clear the old reference otherwise, if we upload an image with a secondary directory and the secondary file
>	' does not require a resize, we will have a lock issue
>	oImage.Dispose()
>
>        ' Take the new result and dump it back to oImage
>        oImage = loImage.Clone
>
>	' Release this object
>	loImage.Dispose()
>
>	Return True
>End Function
>
>
>So, with a System.Drawing.Bitmap object, even if we overwrite the object reference, the old reference remains stuck to it.

Better to use 'Using ..... End Using' - that way Dispose() will be called even if an exception is thrown inside the block :
https://msdn.microsoft.com/en-us/library/htd05whh(v=vs.120).aspx
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform