VB.Net Paste Enhanced Metafile from clipboard to Picture Box -


i want paste enhanced metafile clipboard picture box in vb.net. actually, did that, don't know if doing in right way.

i have object clipboard:

dim obj dataobject = clipboard.getdataobject() dim typ string() typ = objdata.getformats() ' returns "enhancedmetafile" , "metafilepict" 

and want paste picture box.

the following code doesn't work:

    dim objdata dataobject = clipboard.getdataobject()      ' if false     if objdata.getdatapresent(gettype(system.drawing.bitmap))         dim bmp system.drawing.bitmap = ctype(objdata.getdata(gettype(system.drawing.bitmap)), system.drawing.bitmap)         picturebox1.image = bmp     end if 

then tried (that worked, from here)):

imports system.drawing.imaging imports system.runtime.interopservices  public class clipboardmetafilehelper     <dllimport("user32.dll", entrypoint:="openclipboard", _        setlasterror:=true, exactspelling:=true, callingconvention:=callingconvention.stdcall)> _     public shared function openclipboard(byval hwnd intptr) boolean     end function     <dllimport("user32.dll", entrypoint:="emptyclipboard", _        setlasterror:=true, exactspelling:=true, callingconvention:=callingconvention.stdcall)> _     public shared function emptyclipboard() boolean     end function     <dllimport("user32.dll", entrypoint:="setclipboarddata", _        setlasterror:=true, exactspelling:=true, callingconvention:=callingconvention.stdcall)> _     public shared function setclipboarddata(byval uformat integer, byval hwnd intptr) intptr     end function     <dllimport("user32.dll", entrypoint:="getclipboarddata", _        setlasterror:=true, exactspelling:=true, callingconvention:=callingconvention.stdcall)> _     public shared function getclipboarddata(byval uformat integer) intptr     end function     <dllimport("user32.dll", entrypoint:="closeclipboard", _        setlasterror:=true, exactspelling:=true, callingconvention:=callingconvention.stdcall)> _     public shared function closeclipboard() boolean     end function     <dllimport("gdi32.dll", entrypoint:="copyenhmetafilea", _        setlasterror:=true, exactspelling:=true, callingconvention:=callingconvention.stdcall)> _     public shared function copyenhmetafile(byval hemfsrc intptr, byval hnull intptr) intptr     end function     <dllimport("gdi32.dll", entrypoint:="deleteenhmetafile", _        setlasterror:=true, exactspelling:=true, callingconvention:=callingconvention.stdcall)> _     public shared function deleteenhmetafile(byval hemfsrc intptr) boolean     end function      ' metafile mf set state not valid inside function.     public shared function putenhmetafileonclipboard(byval hwnd intptr, byval mf metafile) boolean         dim bresult new boolean()         bresult = false         dim hemf, hemf2 intptr         hemf = mf.gethenhmetafile() ' invalidates mf         if not hemf.equals(new intptr(0))             hemf2 = copyenhmetafile(hemf, new intptr(0))             if not hemf2.equals(new intptr(0))                 if openclipboard(hwnd)                     if emptyclipboard()                         dim hres intptr                         hres = setclipboarddata(14, hemf2)    ' 14 == cf_enhmetafile                         bresult = hres.equals(hemf2)                         closeclipboard()                     end if                 end if             end if             deleteenhmetafile(hemf)         end if         return bresult     end function      public shared function getenhmetafilefromclipboard(byval hwnd intptr) image         openclipboard(hwnd) ' intptr.zero         dim hemf intptr = getclipboarddata(14) ' 14 == cf_enhmetafile         closeclipboard()         if hemf <> intptr.zero             dim mf new metafile(hemf, true)             dim b new bitmap(mf.width, mf.height)             dim g graphics = graphics.fromimage(b)             g.fillrectangle(brushes.white, 0, 0, 1000, 1000)             dim unit graphicsunit = graphicsunit.millimeter             dim rsrc rectanglef = mf.getbounds(unit)             g.drawimage(mf, new rectangle(0, 0, mf.width, mf.height), rsrc, unit)             return b         end if         return nothing     end function  end class 

then call with:

picturebox1.image = clipboardmetafilehelper.getenhmetafilefromclipboard(me.handle) 

the above code works wondering if there way of pasting enhanced metafile without need of dll's?

thanks!!!


Comments