Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Small tip on using images
Message
 
À
10/04/2016 11:53:52
Information générale
Forum:
ASP.NET
Catégorie:
Photos et traitement d'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:
01634675
Vues:
51
There are a number of issues with this code. GDI+ uses unmanaged resources, so properly using Dispose() (or using as Viv points out) is key to ensure you get reliable results. One problem is that sometimes issues don't show up until you're in production because Dispose() timing issues can behave differently depending load and simple timing.

The first is that you have to dispose the image explicitly before writing it again. It may work occasionally depending on how quick the garbage collector gets to the Gen0 collection, but more often than not the object likely isn't disposed.

The second issue is that certain file formats - PNG in particular - can't be rewritten in a single go. So you can't for example, read a PNG from disk, resize it, then write it back in a single Bitmap/Graphics instance. You have to basically open, resize, write out to new (temp) file, close the first file, copy the temp file, delete the new (temp) file.

+++ Rick ---

>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.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform