Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake and show metafiles in VB.NET
DescriptionThis example shows how to make and show metafiles in VB.NET.
Keywordsmetafile, VB.NET
CategoriesVB.NET, Graphics
 
When you click the Make button, the program creates a Graphics object for the form. It gets that object's device context handle (hDC) and uses it to create a metafile. In this example, the metafile's bounding corners are (0, 0)-(210, 210) measured in pixels.

The program creates a Graphics object to work with the metafile and uses its methods to draw.

 
Private Sub btnMake_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnMake.Click
    ' Get a Graphics object representing the screen (for
    ' reference).
    Dim gr As Graphics = Me.CreateGraphics()

    ' Get the Graphics object's hDC.
    Dim hdc As IntPtr = gr.GetHdc()

    ' Make a metafile that can work with the hDC.
    Dim mf As New Metafile(txtFile.Text, hdc, _
        New Rectangle(0, 0, 210, 210), _
        MetafileFrameUnit.Pixel)

    ' Make a Graphics object to work with the metafile.
    Dim mf_gr As Graphics = Graphics.FromImage(mf)

    ' Draw on the metafile.
    Dim the_pen As New Pen(Color.Blue, 5)
    mf_gr.DrawRectangle(the_pen, 5, 5, 190, 190)

    the_pen.Color = Color.Red
    the_pen.Width = 10
    mf_gr.DrawEllipse(the_pen, 20, 20, 160, 160)

    Dim pts() As Point = { _
        New Point(100, 40), _
        New Point(160, 100), _
        New Point(100, 160), _
        New Point(40, 100) _
    }
    the_pen.Color = Color.Green
    the_pen.Width = 3
    mf_gr.DrawPolygon(the_pen, pts)

    the_pen.Color = Color.Orange
    the_pen.Width = 5
    mf_gr.DrawLine(the_pen, 75, 75, 125, 125)
    mf_gr.DrawLine(the_pen, 125, 75, 75, 125)

    ' Clean up.
    mf_gr.Dispose()
    mf.Dispose()
    gr.ReleaseHdc(hdc)
    gr.Dispose()

    MessageBox.Show("Ok")
End Sub
 
When you click the Load button, the program creates a new matefile, passing the constructor the name of the saved file. It makes a Bitmap that's the same size as the metafile, copies the metafile into it, and displays the result.
 
Private Sub btnLoad_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnLoad.Click
    Dim mf As New Metafile(txtFile.Text)
    Dim bm As New Bitmap(mf.Width, mf.Height)
    Dim gr As Graphics = Graphics.FromImage(bm)
    gr.DrawImage(mf, 0, 0)
    picMetafile.Image = bm
    gr.Dispose()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated