Search code examples
system.drawing

Using Block WIth System.Drawing.Graphics


I was wondering if the following code block is safe, meaning will the gfx dispose itself is something goes wrong?

Using gfx As Graphics = Graphics.FromImage(img)
    gfx.Clear(Color.Transparent)
    gfx.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
End Using

Solution

  • Yes, that's the whole point of the using keyword. From here:

    A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.