' 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 FunctionOn 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.
' 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 FunctionSo, with a System.Drawing.Bitmap object, even if we overwrite the object reference, the old reference remains stuck to it.