Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How to save or serialize a Metafile: Solution found.

$
0
0

Hi *, It seems that it is not possible out-of-the box to save or serialize a metafile in .Net

See http://msdn2.microsoft.com/en-us/library/bb882579.aspx.

 

But I have found a workaround. The trick is to use a windows API method called GetEnhMetaFileBits(...).

See code sample below.

 

// We use a Windows API method to obtain the raw data of metafile.

// There seems to be no support in .Net 3.0 for accessing the raw data.

// This is because there is no WMF and EMF encoder available. See http://msdn2.microsoft.com/en-us/library/bb882579.aspx

[System.Runtime.InteropServices.DllImport("gdi32")]

public static extern int GetEnhMetaFileBits(int hemf, int cbBuffer, byte[] lpbBuffer);

 

void DoTheTrick()

{

   Metafile mf = new Metafile();

   int enhMetafileHandle = mf.GetHenhmetafile().ToInt32();

   int bufferSize = GetEnhMetaFileBits(enhMetafileHandle, 0, null); // Get required buffer size.

   byte[] buffer = new byte[bufferSize]; // Allocate sufficient buffer

   if (GetEnhMetaFileBits(enhMetafileHandle, bufferSize, buffer) <= 0) // Get raw metafile data.

      throw new SystemException("DoTheTrick.GetEnhMetaFileBits");

  

   MemoryStream ms = new MemoryStream();

   ms.Write(buffer, 0, bufferSize);

 

// Now do save or serailze the memory stream

//...

}

 

Hope this helps somebody. :-)


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>