Programming Windows with C# Chapter23 Metafiles Presenter: Yanmei Lan Partner: Pratyush Rai
Outline Introductions . Metafile and bitmap compared. Load metafile. Metafile properties. Create metafiles. Metafile Boundary control. Metafile record.
Introductions to Metafiles Metafiles are vector graphics just like bitmaps are raster graphics. A metafile consists of series of binary records that correspond to graphics function calls: to draw lines, curves, filled areas, and text. Metafiles are created by drawing programs. A metafile describes an image in terms of graphical drawing commands that can exist in a disk file or in memory. Metafiles can be stored in a disk or reside in memory.
Metafile and bitmap compared Similarities: 1, Both are used to described images. 2, Both can be stored in disk files . 3,Both inherit from the same abstract class – Image.
Differences Between Bitmap and Metafile 1, Paint programs create bitmaps; drawing program create metafiles, so metafiles describe a picture as a collection of function calls. 2,Bitmaps are from real-world images, but metafiles are constructed by human with computer program. 3, Metafile image can be scaled in size without loss of resolution, bitmaps don’t work that way. 4, A metafile can be converted to a bitmap easily, but converting bitmaps to metafiles is much more difficult.
Metafile Formats and Namespace There are two metafile formats: Windows Metafile – filename.wmf; Enhanced Metafile – filename.emf. Metafile and its related classes and enumeration are defined in the System.Drawing.Imaging namespace.
Class Hierarchy Object MarshalByRefObject Image(abstract) Bitmap Metafile
Loading Existing metafiles You can use FromFile method of Image class to load a metafile from disk: Image image = Image. FromFile(“MyMetafile.emf”); You can create a Metafile object from an existing metafile through Metafile Constructors: The constructors explicitly return an object of type Metafile: Metalfile mf = new Metafile(“MyMetafile.emf”); Metafile( String strFileName) Metafile( Stream stream)
Metafile Sizes and Rendering To display a metafile in its metrical size with the upper left corner at point(x, y): grfx.DrawImage(mf, x, y); To display a metafile stretched to the rectangle: grfx.DrawImage(mf, x, y, cx, cy ); To Display metafile in its pixel size, set page units to pixels and use: grfx.Drawimage(mf, x, y, mf.Width, mf.Height);
Load and Rendering Metafile
MetafileHeader Class MetafileHeader class encapsulate the metafile header that provide additional information about the metafile. You can obtain an object of MetafileHeader through static or nonstatic methods of Metafile class: MetafileHeader mh = mf.GetMetafileHeader ; Metatfileheader mh = Metafile. GetMetafileHeader( “MyPicture.emf);
Image Properties Metafile class inherit properties that describe the image. Image Properties (selection) Type Property Accessibility Size Size get int Width get int Height get float HorizontalResolution get float VerticalResolution get SizeF PhysicalDimension get
MetafileHeader Properties The MetafileHeader class has 10 read only properties that have information about the metafile, page 1106-1107. MetafileHeader Properties(selection) Type Property Accessibility MetafileType Type get int Version get int Metafilesize get Rectangle Bounds get float DpiX get float DpiY get
MetafileType Enumeration The Type property indicates the type of the metafile based on MetafileType enumerration. MetafileType Enumeration Member Value Invalid 0 Wmf 1 WmfPlaceable 2 Emf 3 emfPlusOnly 4 EmfPlusDual 5
Converting Metafiles to Bitmaps Using Bitmap constructor: Bitmap bm = new Bitmap(mf); Creating Bitmap object of particular size then obtain a graphics object to draw on the bitmap: grfx = Graphics.fromImage(bm); grfx.drawImage(mf, 0,0, cx, cy);
How to Create New Metafiles? Use some Metafile constructors with handle argument: Metafile(string strFileName, Intptr ipHdc); Metafile(Stream stream, IntPtr ipHdc); Example: Graphcs grfxVideo = CreateGraphics(); IntPtr ipHdc = grfxVideo.GetDC(); Metafile mf = new Metafile( “NewFile.emf”, ipHdc); grfxVideo.ReleaseDC(ipHdc); grfxVideo. Dispose(); Graphics grfxMetafile = Graphics. FromImage(mf); // add drawing commands in the metafile; grfxMetafile.Dispose();
Examples of Created metafiles
Continue You can also use a MemoryStream object to create the metafile in memory: readonly MemoryStream ms = new MemoryStream(); Metafile mf = new Metafile(ms, ipHdc);
The Metafile Boundary Rectangle You can control over the boundary rectangle of metafile by using other versions of the Metafile constructors with rectangle arguments: Metafile(string strFileName, Inptr ipHdc, Rectangle rect ); Metafile(string strFileName, Inptr ipHdc, RectangleF rectf); Metafile(string steFileName, Inptr ipHdc, rectangle rect, MetafileFrameUnit mfu); Metafile(sting strFileName, Intptr ipHdc, Rectanglef rectf,
MetafileFrameUnit Enumeration The MetafileFrameUnit enumeration indicated the units of the boundary rectangle: Member Value Description Pixel 2 Units of pixels Point 3 Units of 1/72 inch Inch 4 Units of inch Document 5 Units of 1/300 inch Millimeter 6 Units of millimeter GdiCompatible 7 Units of 1/100 millimeter
Metafile Create with Boundary Control
Metafiles and the Page Transform As you add graphics calls to the metafile, coordinates and size are adjusted based on any page transform that is set, see example.
Metafile with Different Transform
Setting Metafile Resolution When a metafile is created, its resolution is set through a device context handle. You can also create a metafile with resolution basing on the printer : PrinterSettings prnset = new PrinterSetting(); Graphics grfx = prnset.CreateMeasureGraphics();
Identify a Metafile Records Each Record of a Metafile is identified by a member of the EmfPlusRecordType enumeration: EmfPlusRecordType Enumeration(selection) Member Value Created version EmfPolyline 4 32-bit versions of Windows DrawLines 16379 Windows Forms program WmfPolyline 66341 32-bit program or Programs before 32-bit version
Create Shorter Metafiles Metafiles can be made shorter by constructors having EmfType arguments. Metafile Constrctors(selestion) Metafile(string strFilename, IntPtr ipHdc, EmfType et) Metafile(string strFileName, IntPtr ipHdc, EmfType et, string strDescription) Metagile(Stream stream, IntPtr ipHdc, EmfType et) Metafile(Intptr iphdc, EmfType et)
EmfType Enumeration EmfType Enumeration Member Value EmfOnly 3 EmfPlusOnly 4 EmfPlusDual 5