Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Small tip on using images
Message
From
10/04/2016 11:53:52
 
 
To
All
General information
Forum:
ASP.NET
Category:
Pictures and Image processing
Title:
Small tip on using images
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:
01634620
Views:
82
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.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform