0

I know that I can get raw metafile bytes from a GDI's HENHMETAFILE using GetEnhMetaFileBits function. But how do I get raw bytes from Gdiplus::Metafile object for GDI+?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • What do you mean by the raw bytes? Do you mean internal storage, or a representation in wmf or emf format? – David Heffernan Jul 17 '19 at 05:44
  • @DavidHeffernan: Something I can use to (de-)serialize a meta-file without the loss of the drawing (vector) data. – c00000fd Jul 17 '19 at 07:13
  • 1
    Use [Metafile::GetHENHMETAFILE](https://learn.microsoft.com/en-us/windows/win32/api/gdiplusheaders/nf-gdiplusheaders-metafile-gethenhmetafile) then you can use GetEnhMetaFileBits on the returned _HENHMETAFILE_ – Castorix Jul 17 '19 at 07:35
  • @Castorix: Thanks. I tried it but unfortunately `GetHENHMETAFILE()` destroys the `Gdiplus::Metafile` object and I can't seem to find an easy way to recreate it. `Gdiplus::Metafile` class doesn't even have a copy constructor or assignment operator. (Unless I'm not seeing it.) In other words, once I serialize it, the source is destroyed. – c00000fd Jul 17 '19 at 07:59
  • Can you post the whole sequence that serializes, but destroys the source? – Tobi Jul 17 '19 at 08:34
  • @Tobi: If you call `Metafile::GetHENHMETAFILE()` method of the `Metafile` object, the `Metafile` object will not be valid afterwards. – c00000fd Jul 17 '19 at 16:52

1 Answers1

1
    public static void Save(Metafile matafile, Stream stream)
    {
        using (stream)
        {
            using (Bitmap bit = new Bitmap(1, 1))
            {
                using (Graphics gs = Graphics.FromImage(bit))
                {
                    Rectangle rt = new Rectangle(0, 0, matafile.Width, matafile.Height);
                    using (Metafile mf = new Metafile(stream, gs.GetHdc(), rt, MetafileFrameUnit.Pixel, EmfType.EmfOnly))
                    {
                        using (Graphics g = Graphics.FromImage(mf))
                        {
                            g.DrawImage(matafile, rt);
                        }
                    }
                }
            }
        }
    }