Skip to main content
.NET Framework Class Library
Metafile Class

Defines a graphic metafile. A metafile contains records that describe a sequence of graphics operations that can be recorded (constructed) and played back (displayed). This class is not inheritable.

Inheritance Hierarchy
SystemObject
   SystemMarshalByRefObject
     System.DrawingImage
      System.Drawing.ImagingMetafile

Namespace:   System.Drawing.Imaging
Assembly:  System.Drawing (in System.Drawing.dll)
Syntax
<[%$TOPIC/7t8z09y3_en-us_VS_110_2_0_0_0_0%]> _
Public NotInheritable Class Metafile _
	Inherits [%$TOPIC/7t8z09y3_en-us_VS_110_2_0_0_0_1%]
[[%$TOPIC/7t8z09y3_en-us_VS_110_2_0_1_0_0%]]
public sealed class Metafile : [%$TOPIC/7t8z09y3_en-us_VS_110_2_0_1_0_1%]
[[%$TOPIC/7t8z09y3_en-us_VS_110_2_0_2_0_0%]]
public ref class Metafile sealed : public [%$TOPIC/7t8z09y3_en-us_VS_110_2_0_2_0_1%]
[<[%$TOPIC/7t8z09y3_en-us_VS_110_2_0_3_0_0%]>]
[<[%$TOPIC/7t8z09y3_en-us_VS_110_2_0_3_0_1%]>]
type Metafile =  
    class 
        inherit [%$TOPIC/7t8z09y3_en-us_VS_110_2_0_3_0_2%] 
    end

The Metafile type exposes the following members.

Constructors
  NameDescription
Public method Metafile(Stream)Initializes a new instance of the Metafile class from the specified data stream.
Public method Metafile(String)Initializes a new instance of the Metafile class from the specified file name.
Public method Metafile(IntPtr, Boolean)Initializes a new instance of the Metafile class from the specified handle.
Public method Metafile(IntPtr, EmfType)Initializes a new instance of the Metafile class from the specified handle to a device context and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(IntPtr, WmfPlaceableFileHeader)Initializes a new instance of the Metafile class from the specified handle and a WmfPlaceableFileHeader.
Public method Metafile(IntPtr, Rectangle)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle.
Public method Metafile(IntPtr, RectangleF)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle.
Public method Metafile(Stream, IntPtr)Initializes a new instance of the Metafile class from the specified data stream.
Public method Metafile(String, IntPtr)Initializes a new instance of the Metafile class with the specified file name.
Public method Metafile(IntPtr, EmfType, String)Initializes a new instance of the Metafile class from the specified handle to a device context and an EmfType enumeration that specifies the format of the Metafile. A string can be supplied to name the file.
Public method Metafile(IntPtr, WmfPlaceableFileHeader, Boolean)Initializes a new instance of the Metafile class from the specified handle and a WmfPlaceableFileHeader. Also, the deleteWmf parameter can be used to delete the handle when the metafile is deleted.
Public method Metafile(IntPtr, Rectangle, MetafileFrameUnit)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle that uses the supplied unit of measure.
Public method Metafile(IntPtr, RectangleF, MetafileFrameUnit)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle that uses the supplied unit of measure.
Public method Metafile(Stream, IntPtr, EmfType)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(Stream, IntPtr, Rectangle)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, and a Rectangle structure that represents the rectangle that bounds the new Metafile.
Public method Metafile(Stream, IntPtr, RectangleF)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, and a RectangleF structure that represents the rectangle that bounds the new Metafile.
Public method Metafile(String, IntPtr, EmfType)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(String, IntPtr, Rectangle)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, and a Rectangle structure that represents the rectangle that bounds the new Metafile.
Public method Metafile(String, IntPtr, RectangleF)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, and a RectangleF structure that represents the rectangle that bounds the new Metafile.
Public method Metafile(IntPtr, Rectangle, MetafileFrameUnit, EmfType)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle that uses the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(IntPtr, RectangleF, MetafileFrameUnit, EmfType)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle that uses the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(Stream, IntPtr, EmfType, String)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, and an EmfType enumeration that specifies the format of the Metafile. Also, a string that contains a descriptive name for the new Metafile can be added.
Public method Metafile(Stream, IntPtr, Rectangle, MetafileFrameUnit)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, a Rectangle structure that represents the rectangle that bounds the new Metafile, and the supplied unit of measure.
Public method Metafile(Stream, IntPtr, RectangleF, MetafileFrameUnit)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, a RectangleF structure that represents the rectangle that bounds the new Metafile, and the supplied unit of measure.
Public method Metafile(String, IntPtr, EmfType, String)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, and an EmfType enumeration that specifies the format of the Metafile. A descriptive string can be added, as well.
Public method Metafile(String, IntPtr, Rectangle, MetafileFrameUnit)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a Rectangle structure that represents the rectangle that bounds the new Metafile, and the supplied unit of measure.
Public method Metafile(String, IntPtr, RectangleF, MetafileFrameUnit)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a RectangleF structure that represents the rectangle that bounds the new Metafile, and the supplied unit of measure.
Public method Metafile(IntPtr, Rectangle, MetafileFrameUnit, EmfType, String)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle that uses the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile. A string can be provided to name the file.
Public method Metafile(IntPtr, RectangleF, MetafileFrameUnit, EmfType, String)Initializes a new instance of the Metafile class from the specified device context, bounded by the specified rectangle that uses the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile. A string can be provided to name the file.
Public method Metafile(Stream, IntPtr, Rectangle, MetafileFrameUnit, EmfType)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, a Rectangle structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(Stream, IntPtr, RectangleF, MetafileFrameUnit, EmfType)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, a RectangleF structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(String, IntPtr, Rectangle, MetafileFrameUnit, EmfType)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a Rectangle structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(String, IntPtr, Rectangle, MetafileFrameUnit, String)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a Rectangle structure that represents the rectangle that bounds the new Metafile, and the supplied unit of measure. A descriptive string can also be added.
Public method Metafile(String, IntPtr, RectangleF, MetafileFrameUnit, EmfType)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a RectangleF structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile.
Public method Metafile(String, IntPtr, RectangleF, MetafileFrameUnit, String)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a RectangleF structure that represents the rectangle that bounds the new Metafile, and the supplied unit of measure. A descriptive string can also be added.
Public method Metafile(Stream, IntPtr, Rectangle, MetafileFrameUnit, EmfType, String)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, a Rectangle structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile. A string that contains a descriptive name for the new Metafile can be added.
Public method Metafile(Stream, IntPtr, RectangleF, MetafileFrameUnit, EmfType, String)Initializes a new instance of the Metafile class from the specified data stream, a Windows handle to a device context, a RectangleF structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile. A string that contains a descriptive name for the new Metafile can be added.
Public method Metafile(String, IntPtr, Rectangle, MetafileFrameUnit, EmfType, String)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a Rectangle structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile. A descriptive string can also be added.
Public method Metafile(String, IntPtr, RectangleF, MetafileFrameUnit, EmfType, String)Initializes a new instance of the Metafile class with the specified file name, a Windows handle to a device context, a RectangleF structure that represents the rectangle that bounds the new Metafile, the supplied unit of measure, and an EmfType enumeration that specifies the format of the Metafile. A descriptive string can also be added.
Top
Properties
  NameDescription
Public property FlagsGets attribute flags for the pixel data of this Image. (Inherited from Image.)
Public property FrameDimensionsListGets an array of GUIDs that represent the dimensions of frames within this Image. (Inherited from Image.)
Public property HeightGets the height, in pixels, of this Image. (Inherited from Image.)
Public property HorizontalResolutionGets the horizontal resolution, in pixels per inch, of this Image. (Inherited from Image.)
Public property PaletteGets or sets the color palette used for this Image. (Inherited from Image.)
Public property PhysicalDimensionGets the width and height of this image. (Inherited from Image.)
Public property PixelFormatGets the pixel format for this Image. (Inherited from Image.)
Public property PropertyIdListGets IDs of the property items stored in this Image. (Inherited from Image.)
Public property PropertyItemsGets all the property items (pieces of metadata) stored in this Image. (Inherited from Image.)
Public property RawFormatGets the file format of this Image. (Inherited from Image.)
Public property SizeGets the width and height, in pixels, of this image. (Inherited from Image.)
Public property TagGets or sets an object that provides additional data about the image. (Inherited from Image.)
Public property VerticalResolutionGets the vertical resolution, in pixels per inch, of this Image. (Inherited from Image.)
Public property WidthGets the width, in pixels, of this Image. (Inherited from Image.)
Top
Methods
  NameDescription
Public method CloneCreates an exact copy of this Image. (Inherited from Image.)
Public method CreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method DisposeReleases all resources used by this Image. (Inherited from Image.)
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method GetBoundsGets the bounds of the image in the specified unit. (Inherited from Image.)
Public method GetEncoderParameterListReturns information about the parameters supported by the specified image encoder. (Inherited from Image.)
Public method GetFrameCountReturns the number of frames of the specified dimension. (Inherited from Image.)
Public method GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public method GetHenhmetafileReturns a Windows handle to an enhanced Metafile.
Public method GetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method GetMetafileHeaderReturns the MetafileHeader associated with this Metafile.
Public method Static member GetMetafileHeader(IntPtr)Returns the MetafileHeader associated with the specified Metafile.
Public method Static member GetMetafileHeader(Stream)Returns the MetafileHeader associated with the specified Metafile.
Public method Static member GetMetafileHeader(String)Returns the MetafileHeader associated with the specified Metafile.
Public method Static member GetMetafileHeader(IntPtr, WmfPlaceableFileHeader)Returns the MetafileHeader associated with the specified Metafile.
Public method GetPropertyItemGets the specified property item from this Image. (Inherited from Image.)
Public method GetThumbnailImageReturns a thumbnail for this Image. (Inherited from Image.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public method PlayRecordPlays an individual metafile record.
Public method RemovePropertyItemRemoves the specified property item from this Image. (Inherited from Image.)
Public method RotateFlipRotates, flips, or rotates and flips the Image. (Inherited from Image.)
Public method Save(String)Saves this Image to the specified file or stream. (Inherited from Image.)
Public method Save(Stream, ImageFormat)Saves this image to the specified stream in the specified format. (Inherited from Image.)
Public method Save(String, ImageFormat)Saves this Image to the specified file in the specified format. (Inherited from Image.)
Public method Save(Stream, ImageCodecInfo, EncoderParameters)Saves this image to the specified stream, with the specified encoder and image encoder parameters. (Inherited from Image.)
Public method Save(String, ImageCodecInfo, EncoderParameters)Saves this Image to the specified file, with the specified encoder and image-encoder parameters. (Inherited from Image.)
Public method SaveAdd(EncoderParameters)Adds a frame to the file or stream specified in a previous call to the Save method. Use this method to save selected frames from a multiple-frame image to another multiple-frame image. (Inherited from Image.)
Public method SaveAdd(Image, EncoderParameters)Adds a frame to the file or stream specified in a previous call to the Save method. (Inherited from Image.)
Public method SelectActiveFrameSelects the frame specified by the dimension and index. (Inherited from Image.)
Public method SetPropertyItemStores a property item (piece of metadata) in this Image. (Inherited from Image.)
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method ISerializableGetObjectDataPopulates a SerializationInfo with the data needed to serialize the target object. (Inherited from Image.)
Top
Remarks

When you use the Save method to save a graphic image as a Windows Metafile Format (WMF) or Enhanced Metafile Format (EMF) file, the resulting file is saved as a Portable Network Graphics (PNG) file instead. This behavior occurs because the GDI+ component of the .NET Framework does not have an encoder that you can use to save files as .wmf or .emf files.

Examples

The following code example demonstrates how to create a Metafile and use the PlayRecord method.

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
' for Marshal.Copy 
Imports System.Runtime.InteropServices


Public Class Form1
    Inherits Form
    Private metafile1 As Metafile
    Private metafileDelegate As Graphics.EnumerateMetafileProc
    Private destPoint As Point

    Public Sub New() 
        metafile1 = New Metafile("C:\test.wmf")
        metafileDelegate = New Graphics.EnumerateMetafileProc(AddressOf MetafileCallback)
        destPoint = New Point(20, 10)

    End Sub 

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 
        e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate)

    End Sub 

    Private Function MetafileCallback(ByVal recordType As _
        EmfPlusRecordType, ByVal flags As Integer, ByVal dataSize As Integer, _
        ByVal data As IntPtr, ByVal callbackData As PlayRecordCallback) As Boolean 

        Dim dataArray As Byte() = Nothing 
        If data <> IntPtr.Zero Then 

            ' Copy the unmanaged record to a managed byte buffer  
            ' that can be used by PlayRecord.
            dataArray = New Byte(dataSize) {}
            Marshal.Copy(data, dataArray, 0, dataSize)
        End If

        metafile1.PlayRecord(recordType, flags, dataSize, dataArray)
        Return True 

    End Function 

    Shared Sub Main() 
        Application.Run(New Form1())
    End Sub 

End Class
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

// for Marshal.Copy 
using System.Runtime.InteropServices; 

public class Form1 : Form
{
    private Metafile metafile1;
    private Graphics.EnumerateMetafileProc metafileDelegate;
    private Point destPoint;
    public Form1()
    {
        metafile1 = new Metafile(@"C:\Test.wmf");
        metafileDelegate = new Graphics.EnumerateMetafileProc(MetafileCallback);
        destPoint = new Point(20, 10);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate);
    }
    private bool MetafileCallback(
       EmfPlusRecordType recordType,
       int flags,
       int dataSize,
       IntPtr data,
       PlayRecordCallback callbackData)
    {
        byte[] dataArray = null;
        if (data != IntPtr.Zero)
        {
            // Copy the unmanaged record to a managed byte buffer  
            // that can be used by PlayRecord.
            dataArray = new byte[dataSize];
            Marshal.Copy(data, dataArray, 0, dataSize);
        }

        metafile1.PlayRecord(recordType, flags, dataSize, dataArray);

        return true;
    }

    static void Main()
    {
        Application.Run(new Form1());
    }
}
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.