I have an object that has a custom "HandlePaint(pe as PaintEventArgs)" that I call downstream from my Form.Paint event. It does some moderately complex rendering, and it is sometimes advantageous (as a speed tweak) to, instead of rendering it normally, render it to a bitmap object (freeze it) and thereafter just paint that bitmap (to avoid the math that goes into the calculating of the appearance). Basically I call the "Freeze" function below, you get the idea.
Quite often (usually when I call "Freeze" in quick succession with other operations -- that might cause repaints; however haven't strongly correlated the problem yet) I run into the exception "Object is Currently In Use Elsewhere" with the description:
"If you are using the Graphics object after the GetHdc method, call the ReleaseHdc method.
This will release the context handle obtained by a previous call to the GetHdc method of the Graphics object. "
However as you can see I am not using GetHdc, I'm using Graphics.Image. But regardless I don't know how I'm hitting this -- my rendering is complex (maybe time consuming), but Freeze command is a discrete function, is it colliding somehow with some framework Paint activity? How is that? Any thoughts on how to bypass this problem and how to avoid it in the future?
Thanks for any help.
Public Sub Handle_Paint(ByVal pe As System.Windows.Forms.PaintEventArgs)
If m_Frozen Then
pe.Graphics.DrawImageUnscaled(m_FrozenBitmap, m_X, m_Y)
' does some moderately complex GDI rendering
Public m_Frozen as Boolean = False
Public m_FrozenBitmap As Bitmap
Public Sub Freeze(ByVal sz As Size)
m_FrozenBitmap = New Bitmap(sz.Width + 1, sz.Height + 1)
Dim g2 As Graphics = Graphics.FromImage(m_FrozenBitmap)
Dim pe As New PaintEventArgs(g2, New Rectangle(0, 0, sz.Width + 1, sz.Height + 1))
m_Frozen = True