Make a metafile in C#

[metafile]

The example Display a Windows metafile (wmf) in C# explains how to display a metafile and why metafiles can produce better results than raster images such as those stored in bmp, jpg, or png formats. This example shows how a C# program can make a metafile.


The example program executes the following MakeMetafile method when it starts.

// Make a metafile containing an epitrochoid.
private void MakeMetafile(string filename)
{
    // Get a Graphics object representing the form (for reference).
    using (Graphics gr = CreateGraphics())
    {
        // Get the Graphics object's hDC. (Released later.)
        IntPtr hDC = gr.GetHdc();

        // Make a metafile that can work with the hDC.
        Rectangle rect = new Rectangle(0, 0, 100, 100);
        Metafile mf = new Metafile(filename,
            hDC, rect, MetafileFrameUnit.Pixel);

        // Make a Graphics object to draw on the Metafile.
        using (Graphics mf_gr = Graphics.FromImage(mf))
        {
            // Smooth.
            mf_gr.SmoothingMode = AntiAlias;

            // Translate and scale to make the
            // image fill the metafile's area.
            mf_gr.ScaleTransform(45, 45);
            mf_gr.TranslateTransform(50, 50, MatrixOrder.Append);
            using (Pen pen = new Pen(Color.Red, 0))
            {
                DrawEpitrochoid(mf_gr, pen, 13, 7, 9, 0.05f);
            }
        }

        // Release the hDC.
        gr.ReleaseHdc(hDC);
    }
}

This code creates a Graphics object for the form. It doesn’t draw on that object, it just uses it to create a compatible metafile.

Next the program calls the Graphics object’s GetHdc method to get a handle to its device context. It is important that the code later releases that handle. Unfortunately device contexts are unmanaged. That means they are not normal .NET objects so they don’t implement the IDisposable interface and you can’t free them with the using statement. Instead you must remember to release device contexts yourself.

The code then creates a Rectangle to define the drawing area that the metafile will represent. It uses that Rectangle and the device context to create the metafile. There are 39 overloaded constructors for the Metafile class. The version used here creates the metafile in a file.

After creating the Metafile object, the program makes a Graphics object associated with it. It then uses normal Graphics class methods to draw on the metafile. The DrawEpitrochoid method does most of the work. See the example Draw an epitrochoid in C# for information about how the program draws the epitrochoid.

After it finishes making the metafile, the program uses the following code to display it.

// Display the metafile.
picMetafile.Image = new Metafile(filename);

Note that all of the graphics operations are saved in the metafile. For example, this program’s DrawEpitrochoid method sets the Graphics object’s SmoothingMode propety to AntiAlias so it produces a smooth curve. The metafile saves that command so when other programs play the metafile they can make the result smooth, too.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in drawing, files, graphics, image processing and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *