none
How does the function "Graphics.Save()" work in save as mef? RRS feed

  • Question

  • I search a way to save the graphics in a "EMF" file.

    I got the following code. It really work. I can get a vector graphics file. but i cannot understand how the function work. can someone help me?

                    Bitmap bmp = new Bitmap(Width, Height);
                    Graphics gs = Graphics.FromImage(bmp);
                    Metafile mf = new Metafile(filePath, gs.GetHdc(), ClientRectangle, MetafileFrameUnit.Pixel);
                    Graphics g = Graphics.FromImage(mf);

                    g.DrawRectangle(Pens.Black, new Rectangle(new Point(10, 10), new Size(30, 30)));

                    g.Save();

                    g.Dispose();
                    mf.Dispose();
                    gs.Dispose();
                    bmp.Dispose();

    Thursday, April 30, 2015 8:00 AM

Answers

  • That's not what I see here if I run your code. The .emf file is created as soon as the Metafile constructor runs.
    • Marked as answer by Andrew, Yuan Thursday, April 30, 2015 8:49 AM
    Thursday, April 30, 2015 8:36 AM
    Moderator

All replies

  • Graphics.Save has nothing to do with metafiles and with what you're trying to do. Graphics.Save saves the Graphics's object state to a GraphicsState object (which you don't even use in your code) and that object is supposed to be used in a call to Graphics.Restore - see the MSDN documentation.

    You're specifying a path when creating the metafile object, the file is automatically created without calling any sort of "Save" function.

    Thursday, April 30, 2015 8:15 AM
    Moderator
  • That's not what I see here if I run your code. The .emf file is created as soon as the Metafile constructor runs.
    • Marked as answer by Andrew, Yuan Thursday, April 30, 2015 8:49 AM
    Thursday, April 30, 2015 8:36 AM
    Moderator
  • sorry, you are right. the problem is in my other code.
    Thursday, April 30, 2015 8:49 AM