I am trying to draw GDI+ graphics to a metafile (becuase I need high-resolution resizable vector graphics and bitmaps are not good enough) to put on the clipboard. Documentation seems sketchy on this subject so Im hoping someone can help me. Below is an example code snippet I tried but nothing ends up on the clipboard - am I putting the metafile on the clipboard wrong. Also the gr.dispose fails because the object is in use. Anyone have any advice? Thanks.
Sub CopyGraphicsToClipboard()
Create a bitmap and create a graphics instance from it
Dim b As New Bitmap(675, 450, pixelFormat.Format32bppArgb)
Dim gr As Graphics = Graphics.FromImage(b)
Create a metafile object based on the bitmap-graphics object
and then create a graphics object from the metafile object
Dim mf As New Metafile(Graph.GetHdc, EmfType.EmfOnly)
Dim mfgr As Graphics = Graphics.FromImage(mf)
Draw a simple rectangle to the metafile graphics object
mfgr.DrawRectangle(Pens.Black, 100, 100, 300, 200)
Set the metafile to a dataobject and put it on the clipboard
Dim d As New DataObject
d.SetData(mf.GetType, mf)
Clipboard.SetDataObject(d)
*** Also tried the following but doesnt work either
*** Clipboard.SetDataObject(mf)
Clean up
mfgr.Dispose()
mf.Dispose()
b.Dispose()
gr.Dispose() this fails with msg that object is being used
End Sub
Sub CopyGraphicsToClipboard()
Create a bitmap and create a graphics instance from it
Dim b As New Bitmap(675, 450, pixelFormat.Format32bppArgb)
Dim gr As Graphics = Graphics.FromImage(b)
Create a metafile object based on the bitmap-graphics object
and then create a graphics object from the metafile object
Dim mf As New Metafile(Graph.GetHdc, EmfType.EmfOnly)
Dim mfgr As Graphics = Graphics.FromImage(mf)
Draw a simple rectangle to the metafile graphics object
mfgr.DrawRectangle(Pens.Black, 100, 100, 300, 200)
Set the metafile to a dataobject and put it on the clipboard
Dim d As New DataObject
d.SetData(mf.GetType, mf)
Clipboard.SetDataObject(d)
*** Also tried the following but doesnt work either
*** Clipboard.SetDataObject(mf)
Clean up
mfgr.Dispose()
mf.Dispose()
b.Dispose()
gr.Dispose() this fails with msg that object is being used
End Sub