Metafile Objects


The Metafile class represents image data defined by metafile records. These records encapsulate typical graphics commands that scale, rotate, draw lines, display text, and so forth. Using a Metafile object, you can build the metafile records and save them into a metafile, load a metafile, and play the metafile records on a display surface such as a Bitmap.

Many of the Metafile’s most useful properties and methods are inherited from the Image class. These include Height, HorizontalResolution, Palette, RawFormat, Size, Width, GetThumbnailImage, RotateFlip, and Save. See the section “Image” earlier in this chapter for information about those and other inherited properties and methods.

The following table describes some of the most useful methods that the Metafile class adds to those inherited from the Image class.

Open table as spreadsheet

Method

Purpose

GetMetafileHeader

Returns the MetafileHeader object associated with this Metafile. See the following text for more information on the MetafileHeader class.

PlayRecord

Plays a metafile record. To play the whole metafile, you can use a Graphics object’s DrawImage method to copy the metafile’s image onto a Bitmap and then display the Bitmap. PlayRecord lets you selectively play metafile records.

To build a Metafile, you create a Metafile object, attach a Graphics object to it, and then use drawing methods to draw into the metafile. In that respect, the Metafile behaves just like a Bitmap does.

The Graphics object also provides two special methods for working with its Metafile. AddMetafileComment adds a comment to the metafile. EnumerateMetafile sends the metafile’s records to a callback subroutine one at a time. You can use that routine if you want to play back only some of the Metafile’s records.

The following code shows how to make and use a metafile. It starts by calculating a file name for the metafile. If the file already exists, the program deletes it. Next the program makes a Graphics object to get a handle to its device context. It uses that handle as a parameter to the Metafile object’s constructor. It also passes the constructor the name of the file, a RectangleF that defines the metafile’s bounds, and the units used by the bounds.

After creating the Metafile object, the program attaches a Graphics object to it. It uses the Graphics object to clear the metafile in white and to draw a circle and two lines.

The program then disposes of the Graphics and Metafile objects. That closes the metafile.

The program then calls the Metafile constructor again, passing it the file’s name. It makes a Bitmap and associated Graphics object, and defines source and destination RectangleF structures to use when copying the image. It enlarges the source rectangle slightly so the metafile doesn’t crop off the circle’s right and bottom pixels.

Next, the code uses the Graphics object’s DrawImage method to copy the metafile onto the Bitmap. It then sets the picOrig control’s Image property to the Bitmap to display the result.

The program then repeats these steps to display the metafile on the picSmall control. This time it makes the control half as large as the full-scale image and uses a scaling transformation to shrink the metafile data when it calls DrawImage.

  Private Sub Form1_Load(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load     ' Make a bitmap.     Const WID As Integer = 200     Dim bm As New Bitmap(WID, WID)     ' Find the WMF file's path and delete      Dim path_name As String = Application.StartupPath     If path_name.EndsWith("\bin") Then _         path_name = path_name.Substring(0, path_name.Length - 4)     Dim file_name As String = path_name & "\test.wmf"     If Len(Dir$(file_name)) > 0 Then Kill(file_name)     ' Make a Graphics object so we can use its hDC as a reference.     Using me_gr As Graphics = Me.CreateGraphics         Dim me_hdc As IntPtr = me_gr.GetHdc         ' Make the Metafile, using the reference hDC.         Dim bounds As New RectangleF(0, 0, WID, WID)         Using mf As New Metafile(file_name, me_hdc, _             bounds, MetafileFrameUnit.Pixel)             me_gr.ReleaseHdc(me_hdc)             ' Make a Graphics object and draw.             Using gr As Graphics = Graphics.FromImage(mf)                 gr.PageUnit = GraphicsUnit.Pixel                 gr.Clear(Color.White)                 Using thick_pen As New Pen(Color.Red, 5)                     gr.DrawEllipse(thick_pen, bounds)                     thick_pen.Color = Color.Green                     gr.DrawLine(thick_pen, 0, 0, WID, WID)                     thick_pen.Color = Color.Blue                     gr.DrawLine(thick_pen, WID, 0, 0, WID)                 End Using             End Using ' gr         End Using ' mf         ' Reload the metafile and copy it into a Bitmap.         Using mf As New Metafile(file_name)             Using gr As Graphics = Graphics.FromImage(bm)                 Dim dest_bounds As New RectangleF(0, 0, WID, WID)                 Dim source_bounds As New RectangleF(0, 0, WID + 1, WID + 1)                 gr.DrawImage(mf, bounds, source_bounds, GraphicsUnit.Pixel)                 picOrig.SizeMode = PictureBoxSizeMode.AutoSize                 picOrig.Image = bm             End Using ' gr         End Using ' mf         ' Redisplay the result shrunk by 50%.         Using mf As New Metafile(file_name)             picSmall.SetBounds( _                 picOrig.Right + 10, picOrig.Top, _                 picOrig.Width \ 2, picOrig.Height \ 2)             bm = New Bitmap( _                 picSmall.ClientSize.Width, _                 picSmall.ClientSize.Height)             Using gr As Graphics = Graphics.FromImage(bm)                 Dim source_bounds As New RectangleF(0, 0, WID + 1, WID + 1)                 gr.ScaleTransform(0.5, 0.5)                 gr.DrawImage(mf, bounds, source_bounds, GraphicsUnit.Pixel)                 picSmall.Image = bm                 gr.Dispose()             End Using ' gr         End Using ' mf     End Using ' me_gr End Sub 

Figure 23-3 shows the result.

image from book
Figure 23-3: Program MakeMetafile creates a metafile and then draws two copies of it.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net