Metafile fails to exit recording mode -
01-15-2007
, 07:16 AM
Hi
I have a small pice of code that creates a metafile, writes one line of text and returns the metafile.
This only crashes when it is called more than around 29028-29039 times.
Trying to read any of the properties of the metafile results in a "Parameter is not valid." error. This is the same behavior as the metaimage usually have before the graphics object is disposed.
From MSDN:
"The recording of the metafile ends when that Graphics object is deleted or goes out of scope."
This seems to stop function....
My code:
Private Function getMetaWithText(ByVal gTarget As Graphics, ByVal s As String, ByVal font As Font) As Metafile
Dim hRefDC As System.IntPtr = gTarget.GetHdc()
Dim meta As New Metafile(hRefDC, EmfType.EmfPlusOnly)
Using g As Graphics = Graphics.FromImage(meta), b As New SolidBrush(Color.Black)
g.DrawString(s, font, b, 0, 0)
End Using
gTarget.ReleaseHdc(hRefDC)
Return meta
End Function
The Using construct guarantees that the graphics object defined within the Using block will be disposed of after the block. |