Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Windows with C# Chapter23 Metafiles

Similar presentations


Presentation on theme: "Programming Windows with C# Chapter23 Metafiles"— Presentation transcript:

1 Programming Windows with C# Chapter23 Metafiles
Presenter: Yanmei Lan Partner: Pratyush Rai

2 Outline Introductions . Metafile and bitmap compared. Load metafile.
Metafile properties. Create metafiles. Metafile Boundary control. Metafile record.

3 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.

4 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.

5 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.

6 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.

7 Class Hierarchy Object MarshalByRefObject Image(abstract) Bitmap
Metafile

8 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)

9 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);

10 Load and Rendering Metafile

11 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);

12 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

13 MetafileHeader Properties
The MetafileHeader class has 10 read only properties that have information about the metafile, page MetafileHeader Properties(selection) Type Property Accessibility MetafileType Type get int Version get int Metafilesize get Rectangle Bounds get float DpiX get float DpiY get

14 MetafileType Enumeration
The Type property indicates the type of the metafile based on MetafileType enumerration. MetafileType Enumeration Member Value Invalid Wmf WmfPlaceable Emf emfPlusOnly EmfPlusDual

15 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);

16 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();

17 Examples of Created metafiles

18 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);

19 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,

20 MetafileFrameUnit Enumeration
The MetafileFrameUnit enumeration indicated the units of the boundary rectangle: Member Value Description Pixel Units of pixels Point Units of 1/72 inch Inch Units of inch Document Units of 1/300 inch Millimeter Units of millimeter GdiCompatible Units of 1/100 millimeter

21 Metafile Create with Boundary Control

22 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.

23 Metafile with Different Transform

24 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();

25 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 bit versions of Windows DrawLines Windows Forms program WmfPolyline bit program or Programs before 32-bit version

26 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)

27 EmfType Enumeration EmfType Enumeration Member Value EmfOnly 3
EmfPlusOnly EmfPlusDual


Download ppt "Programming Windows with C# Chapter23 Metafiles"

Similar presentations


Ads by Google