I have written a procedure to resize jpgs, it works well apart from the fact that it leaves the input file locked by an IIS worker process.
I'm assuming I need to improve the garbage collection but I can't see what I'm leaving behind. At the moment I have to restart IIS to unlock the file.
Public Sub ResizeImage(ByVal FileInput As String, _
ByVal FileOutput As String, _
ByVal MaxSize As Integer, _
Optional ByVal ShrinkOnly As Boolean = True,
Optional ByVal DeleteInput As Boolean = True)
If My.Computer.FileSystem.FileExists(FileInput) = True Then
Dim isLandscape As Boolean
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(FileInput)
Dim srcWidth As Integer = image.Width
Dim srcHeight As Integer = image.Height
If srcWidth > srcHeight Then
isLandscape = True
Else
isLandscape = False
End If
Dim FileWidth As Integer
Dim FileHeight As Integer
Dim ProcessFile As Boolean = True
If (srcWidth <= MaxSize) And (srcHeight <= MaxSize) Then
If ShrinkOnly = False Then
ProcessFile = True
Else
ProcessFile = False
My.Computer.FileSystem.CopyFile(FileInput, FileOutput)
End If
Else
End If
If ProcessFile = True Then
If isLandscape = True Then
FileWidth = MaxSize
FileHeight = (srcHeight / srcWidth) * MaxSize
Else
FileHeight = MaxSize
FileWidth = (srcWidth / srcHeight) * MaxSize
End If
Dim tn As New System.Drawing.Bitmap(FileWidth, FileHeight)
Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(tn)
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default
Dim rectDestination As New System.Drawing.Rectangle(0, 0, FileWidth, FileHeight)
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, System.Drawing.GraphicsUnit.Pixel)
tn.Save(FileOutput, System.Drawing.Imaging.ImageFormat.Jpeg)
gr.Dispose()
tn.Dispose()
If DeleteInput = True Then
My.Computer.FileSystem.DeleteFile(FileInput)
Else
End If
End If
End If
End Sub
You're not disposing the image variable. Either call Dispose on it when you're done, or wrap it in a Using statement:
Using image As System.Drawing.Image = System.Drawing.Image.FromFile(FileInput)
' Code here
End Using
The latter option is preferred (and is what you should use for your other disposable variables as well) as it ensures proper clean up even when your code crashes.
Member
1 Points
36 Posts
Image resize leaving file locked by IIS worker process
Jun 30, 2012 05:38 PM|Danny Seager|LINK
I have written a procedure to resize jpgs, it works well apart from the fact that it leaves the input file locked by an IIS worker process.
I'm assuming I need to improve the garbage collection but I can't see what I'm leaving behind. At the moment I have to restart IIS to unlock the file.
Participant
1682 Points
488 Posts
ASPInsiders
MVP
Re: Image resize leaving file locked by IIS worker process
Jul 01, 2012 04:13 AM|Imar_Spaanjaars|LINK
Hi Danny,
You're not disposing the image variable. Either call Dispose on it when you're done, or wrap it in a Using statement:
The latter option is preferred (and is what you should use for your other disposable variables as well) as it ensures proper clean up even when your code crashes.
Hope this helps,
Imar
My Blog - My Company