Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. My metafile code, what did I lose?

My metafile code, what did I lose?

Scheduled Pinned Locked Moved Visual Basic
graphicshelpquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astcws
    wrote on last edited by
    #1

    The code below is I copy from a book , and try to change using the memorystream. However, it didn't work. What thing I lost? Can someone help me? Thanks very much. Dim stream As New MemoryStream Dim m As Metafile Dim g As Graphics = Me.CreateGraphics Dim _hdc As IntPtr _hdc = g.GetHdc m = New Metafile(stream, _hdc) g.ReleaseHdc() Dim gr As Graphics = Graphics.FromImage(m) gr.PageUnit = GraphicsUnit.Pixel gr.Clear(Color.White) gr.DrawEllipse(Pens.Red, Bounds) gr.DrawLine(Pens.Blue, 0, 0, 100, 100) gr.DrawLine(Pens.Blue, 100, 0, 0, 100) gr.Dispose() m.Dispose() m = New Metafile(stream) 'break in here. Dim bm As New Bitmap(100, 100) gr = Graphics.FromImage(bm) Dim dest_bounds As New RectangleF(0, 0, 100, 100) Dim source_bounds As New RectangleF(0, 0, 100, 100) gr.DrawImage(m, Bounds, source_bounds, GraphicsUnit.Pixel) pic.SizeMode = PictureBoxSizeMode.AutoSize pic.Image = bm gr.Dispose() m.Dispose()

    D 1 Reply Last reply
    0
    • A astcws

      The code below is I copy from a book , and try to change using the memorystream. However, it didn't work. What thing I lost? Can someone help me? Thanks very much. Dim stream As New MemoryStream Dim m As Metafile Dim g As Graphics = Me.CreateGraphics Dim _hdc As IntPtr _hdc = g.GetHdc m = New Metafile(stream, _hdc) g.ReleaseHdc() Dim gr As Graphics = Graphics.FromImage(m) gr.PageUnit = GraphicsUnit.Pixel gr.Clear(Color.White) gr.DrawEllipse(Pens.Red, Bounds) gr.DrawLine(Pens.Blue, 0, 0, 100, 100) gr.DrawLine(Pens.Blue, 100, 0, 0, 100) gr.Dispose() m.Dispose() m = New Metafile(stream) 'break in here. Dim bm As New Bitmap(100, 100) gr = Graphics.FromImage(bm) Dim dest_bounds As New RectangleF(0, 0, 100, 100) Dim source_bounds As New RectangleF(0, 0, 100, 100) gr.DrawImage(m, Bounds, source_bounds, GraphicsUnit.Pixel) pic.SizeMode = PictureBoxSizeMode.AutoSize pic.Image = bm gr.Dispose() m.Dispose()

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      There are so many problems with your code, I think I'll just rewrite it...

      ' Something to hold onto our memory stream
      Private metaMemoryStream As New MemoryStream

      Private Sub Button1_Click(blah, blah) Handles Button1.Click
      Using g As Graphics = Me.CreateGraphics()
      Dim hdc As IntPtr = g.GetHdc
      Using meta As New MetaFile(metaMemoryStream, hdc)
      Using metaGraphics As Graphics = Graphics.FromImage(meta)
      '
      ' Draw your stuff here...
      '
      metaGraphics.Draw...
      '
      '
      '
      End Using
      g.ReleaseHdc(hdc)
      End Using
      End Using
      End Sub

      Private Sub ShowMetaFile()
      ' VERY IMPORTANT!
      ' Make sure that we start at the beginning of the Memory Stream!!
      metaMemoryStream.Seek(0, SeekOrigin.Begin)
      PictureBox1.Image = Image.FromStream(metaMemoryStream)
      End Sub

      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        There are so many problems with your code, I think I'll just rewrite it...

        ' Something to hold onto our memory stream
        Private metaMemoryStream As New MemoryStream

        Private Sub Button1_Click(blah, blah) Handles Button1.Click
        Using g As Graphics = Me.CreateGraphics()
        Dim hdc As IntPtr = g.GetHdc
        Using meta As New MetaFile(metaMemoryStream, hdc)
        Using metaGraphics As Graphics = Graphics.FromImage(meta)
        '
        ' Draw your stuff here...
        '
        metaGraphics.Draw...
        '
        '
        '
        End Using
        g.ReleaseHdc(hdc)
        End Using
        End Using
        End Sub

        Private Sub ShowMetaFile()
        ' VERY IMPORTANT!
        ' Make sure that we start at the beginning of the Memory Stream!!
        metaMemoryStream.Seek(0, SeekOrigin.Begin)
        PictureBox1.Image = Image.FromStream(metaMemoryStream)
        End Sub

        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        A Offline
        A Offline
        astcws
        wrote on last edited by
        #3

        Yeah!!! Thank you! Thank you! very much... I have been confused many days about this problem until you taught me the correct way. Really thank you again! I don't know how to describe my happyiness.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups