My metafile code, what did I lose?
-
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()
-
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()
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 MemoryStreamPrivate 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 SubPrivate 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 SubDave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
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 MemoryStreamPrivate 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 SubPrivate 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 SubDave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007