Is it possible to continue editing a metafile which is stored in a variable

STL 21 Reputation points
2021-01-19T10:30:43.59+00:00

I have created a metafile and drawn a few lines into it.
After that I stored the metafile in a variable.

Dim myImage as Metafile
.
.
System(0).Image = myImage

When I try to load the metafile again to extend it

myImage = System(0).Image
Dim gr as Graphics = Graphics.FromImage(myImage)

a System.OutOfMemoryException pops up

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,578 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-01-20T06:36:44.15+00:00

    Hi @STL ,
    You can consider saving Metafile as EMF, and then reload it.
    Here's an example you can refer to.

        <DllImport("gdi32.dll")>  
        Private Shared Function CopyEnhMetaFile(ByVal hemfSrc As IntPtr, ByVal lpszFile As String) As IntPtr  
        End Function  
        <DllImport("gdi32.dll")>  
        Private Shared Sub DeleteEnhMetaFile(ByVal hemf As IntPtr)  
        End Sub  
      
        Public Function ReloadMetaFile(ByVal mf As Metafile, ByVal fileName As String) As Metafile  
      
            Dim iptrMetafileHandle As IntPtr = mf.GetHenhmetafile()  
            CopyEnhMetaFile(iptrMetafileHandle, fileName)  
            DeleteEnhMetaFile(iptrMetafileHandle)  
      
            Dim bm As Bitmap = New Bitmap(16, 16)  
            Using gr As Graphics = Graphics.FromImage(bm)  
                mf = New Metafile(fileName, gr.GetHdc())  
                gr.ReleaseHdc()  
            End Using  
      
            Return mf  
        End Function  
    

    Use it:

            Dim myImage As Metafile = ...  
            Dim mf As Metafile = ReloadMetaFile(myImage, "EMF file path")  
            Dim gr As Graphics = Graphics.FromImage(mf)  
            ...  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.