I wanted to do a speed test in order to see how costly Graphics.FromImage() is.
To do that I first did a loop into which I repeatedly call Graphics.FromImage. Now in my second test, I do it only once, but now I get an error.
m_Buffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
m_g = Graphics.FromImage(m_Buffer)
For i As Integer = 0 To 1000
Using m_g
m_g.FillEllipse(New SolidBrush(Color.Blue), 0, 0, 100, 100)
End Using
Next i
On (i=1) I get the error "Invalid parameters" on the line m_g.FilleEllipse Does anybody see why?
Perhaps you should do this
....
Using m_g
For i As Integer = 0 To 1000
Using blue As New SolidBrush(Color.Blue)
m_g.FillEllipse(blue, 0, 0, 100, 100)
End Using
Next i
End Using
....
the reference to Graphics object should be disposed after the loop, the reference to the SolidBrush inside the loop.
EDIT:
Using is very important. In particular when you deal with system resources like graphic handles or database connections. Using is a substitute for the try/finally clause with automatic dispose of your declared object at the End Using. This guarantees that, even in case of exceptions, your system resources will be disposed properly without waiting for the Garbage Collector pass.
In the loop above the 1000 SolidBrush objects (and the underlying system graphics handles) will be fred immediately and without putting pressure on the system graphic memory.