public static void Save(this Metafile metafile, Stream stream, bool forceWmfFormat) { if (metafile == null) { throw new ArgumentNullException(nameof(metafile), PublicResources.ArgumentNull); } if (stream == null) { throw new ArgumentNullException(nameof(stream), PublicResources.ArgumentNull); } if (!OSUtils.IsWindows) { throw new PlatformNotSupportedException(Res.RequiresWindows); } bool isWmf = metafile.RawFormat.Guid == ImageFormat.Wmf.Guid; if (isWmf || forceWmfFormat) { WriteWmfHeader(metafile, stream); } // making a clone, otherwise, it will not be usable after saving metafile = (Metafile)metafile.Clone(); IntPtr handle = metafile.GetHenhmetafile(); try { byte[] buffer = isWmf ? Gdi32.GetWmfContent(handle) : (forceWmfFormat ? Gdi32.GetWmfContentFromEmf(handle) : Gdi32.GetEmfContent(handle)); stream.Write(buffer, 0, buffer.Length); } finally { if (isWmf) { Gdi32.DeleteMetaFile(handle); } else { Gdi32.DeleteEnhMetaFile(handle); } metafile.Dispose(); } }