Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Small tip on using images
Message
De
10/04/2016 11:53:52
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Photos et traitement d'images
Titre:
Small tip on using images
Versions des environnements
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01634620
Message ID:
01634620
Vues:
81
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
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform