vba - vb6 screen capture GdipSaveImageToFile similar function for byte array -


in visual basic 6, have following code tackes screen capture , encode or converts jpg, in file. (eg. lres = gdipsaveimagetofile saves jpg file dont want save file instead jpg should saved in memory or in byte array)

i want save jpg image in memory or byte array. should do.

i dont want save png in memory encoded jpg in memory, have search lot till not found solution.

public sub desktoptojpg(byval filename string, optional byval quality long = 80, optional includemousecursor boolean = false) on error resume next      dim tsi gdiplusstartupinput     dim lres long, lgdip long, lbitmap long     dim x long, y long, wide long, high long     dim mydib long, mydc long, desktopdc long     dim bi24bitinfo bitmapinfo     dim bitmapdata() byte     dim pcin pcursorinfo     dim piinfo iconinfo     ' starting position/size of capture (full screen)     x = 0: y = 0     wide = screen.width / screen.twipsperpixelx     high = screen.height / screen.twipsperpixely     '     bi24bitinfo.bmiheader       .bibitcount = 24       .bicompression = bi_rgb       .biplanes = 1       .bisize = len(bi24bitinfo.bmiheader)       .biwidth = wide       .biheight = high       .bidatasize = ((.biwidth * 3 + 3) , &hfffffffc) * .biheight       redim bitmapdata(0 .bidatasize - 1)     end     frmscrcontrol.caption = ubound(bitmapdata)     mydc = createcompatibledc(0)     mydib = createdibsection(mydc, bi24bitinfo, dib_rgb_colors, byval 0&, byval 0&, byval 0&)     selectobject mydc, mydib     desktopdc = getdc(0)     bitblt mydc, 0, 0, bi24bitinfo.bmiheader.biwidth, bi24bitinfo.bmiheader.biheight, desktopdc, x, y, vbsrccopy or captureblt     ' include mouse cursor?     if includemousecursor = true         pcin.cbsize = len(pcin)         getcursorinfo pcin         geticoninfo pcin.hcursor, piinfo         drawicon mydc, pcin.ptscreenpos.x - piinfo.xhotspot, pcin.ptscreenpos.y - piinfo.yhotspot, pcin.hcursor         if piinfo.hbmmask deleteobject piinfo.hbmmask         if piinfo.hbmcolor deleteobject piinfo.hbmcolor     end if     call getdibits(mydc, mydib, 0, bi24bitinfo.bmiheader.biheight, bitmapdata(0), bi24bitinfo, dib_rgb_colors)       ' save jpg    '------------    'initialize gdi+    tsi.gdiplusversion = 1    lres = gdiplusstartup(lgdip, tsi)    if lres = 0       ' create gdi+ bitmap image handle       lres = gdipcreatebitmapfromhbitmap(mydib, 0, lbitmap)       if lres = 0          dim tjpgencoder guid          dim tparams encoderparameters          ' initialize encoder guid          clsidfromstring strptr("{557cf401-1a04-11d3-9a73-0000f81ef32e}"), tjpgencoder          ' initialize encoder parameters          tparams.count = 1          tparams.parameter ' quality             ' set quality guid             clsidfromstring strptr("{1d5be4b5-fa4a-452d-9cdd-5db35105e7eb}"), .guid             .numberofvalues = 1             .type = 4             .value = varptr(quality)          end          ' save image          lres = gdipsaveimagetofile(lbitmap, strptr(filename), tjpgencoder, tparams)          ' destroy bitmap          gdipdisposeimage lbitmap       end if       ' shutdown gdi+       gdiplusshutdown lgdip    end if     if lres       err.raise 5, , "cannot save image. gdi+ error:" & lres    end if     ' clean     releasedc 0, desktopdc     deleteobject mydib     deletedc mydc end sub 

you can use gdipsaveimagetostream, copy data vb array.

you have use tlb referencing istream.

it took me while find tlb ; can downloaded here: http://www.vbaccelerator.com/home/vb/type_libraries/stream/vbstrm_type_library.asp (you have add tlb reference project).

on this vb forum, found code convert stream vb array :

option explicit  ' note parameter type changes... private declare function gdipsaveimagetostream lib "gdiplus" (byval image long, byval stream iunknown, clsidencoder any, encoderparams any) long private declare function gdiploadimagefromstream lib "gdiplus" (byval stream iunknown, image long) long  private declare function createstreamonhglobal lib "ole32" (byval hglobal long, byval fdeleteonrelease long, ppstm any) long private declare function globalalloc lib "kernel32" (byval uflags long, byval dwbytes long) long private declare function gethglobalfromstream lib "ole32" (byval ppstm long, hglobal long) long private declare function globallock lib "kernel32" (byval hmem long) long private declare function globalunlock lib "kernel32" (byval hmem long) long private declare function globalsize lib "kernel32" (byval hmem long) long  public function istreamfromarray(byval arrayptr long, byval length long) stdole.iunknown      ' purpose: create istream-compatible iunknown interface containing     ' passed byte aray. iunknown interface can passed gdi+ functions     ' expect istream interface -- neat hack      ' arrayptr: passed varptr(myarray(0))     ' length: total bytes read arrayptr      on error goto handleerror     dim o_hmem long     dim o_lpmem  long      if arrayptr = 0&         createstreamonhglobal 0&, 1&, istreamfromarray     elseif length <> 0&         o_hmem = globalalloc(&h2&, length)         if o_hmem <> 0             o_lpmem = globallock(o_hmem)             if o_lpmem <> 0                 copymemory byval o_lpmem, byval arrayptr, length                 call globalunlock(o_hmem)                 call createstreamonhglobal(o_hmem, 1&, istreamfromarray)             end if         end if     end if  handleerror: end function  public function istreamtoarray(byval hstream long, arraybytes() byte) boolean      ' return array contained in iunknown interface (stream)      ' hstream: passed objptr(istream) istream declared iunknown     ' arraybytes(): empty byte array; lbound 0      dim o_hmem long, o_lpmem long     dim o_lngbytecount long      if hstream         if gethglobalfromstream(byval hstream, o_hmem) = 0             o_lngbytecount = globalsize(o_hmem)             if o_lngbytecount > 0                 o_lpmem = globallock(o_hmem)                 if o_lpmem <> 0                     redim arraybytes(0 o_lngbytecount - 1)                     copymemory arraybytes(0), byval o_lpmem, o_lngbytecount                     globalunlock o_hmem                     istreamtoarray = true                 end if             end if         end if     end if  end function 

notice iunknown used generic type istream.

hope helps.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -