Capture an image using DirectShow via webcam in vb.net -


so i'm using project webcam stream on form. want capture frame , save now. how this. appreciated.

imports system imports system.diagnostics imports system.drawing imports system.runtime.interopservices imports system.windows.forms imports directshowlib imports system.runtime.interopservices.comtypes  namespace capture_the_webcam     public class form1         inherits system.windows.forms.form         enum playstate             stopped             paused             running             init         end enum         dim currentstate playstate = playstate.stopped          dim d integer = convert.toint32("0x8000", 16)         public wm_graphnotify integer = d + 1          dim videowindow ivideowindow = nothing         dim mediacontrol imediacontrol = nothing         dim mediaeventex imediaeventex = nothing         dim graphbuilder igraphbuilder = nothing         dim capturegraphbuilder icapturegraphbuilder2 = nothing          dim rot dsrotentry = nothing          <stathread()> shared sub main()             application.run(new form1)         end sub         private sub form1_load(byval sender object, byval e system.eventargs) handles me.load             initializecomponent()             capturevideo()         end sub          private sub initializecomponent()             dim resources system.resources.resourcemanager = new system.resources.resourcemanager(gettype(form1))             me.autoscalebasesize = new system.drawing.size(5, 13)             me.clientsize = new system.drawing.size(320, 320)             me.icon = ctype((resources.getobject("$this.icon")), system.drawing.icon)             me.name = "form1"             me.text = "video capture previewer (playcap)"             debug.writeline("i started sub initializecomponent")         end sub         private sub capturevideo()             dim hr integer = 0             dim sourcefilter ibasefilter = nothing             try                 getinterfaces()                  hr = me.capturegraphbuilder.setfiltergraph(me.graphbuilder) 'specifies filter graph "graphbuilder" capture graph builder "capturegraphbuilder" use.                 debug.writeline("attach filter graph capture graph : " & dserror.geterrortext(hr))                 dserror.throwexceptionforhr(hr)                  sourcefilter = findcapturedevice()                  hr = me.graphbuilder.addfilter(sourcefilter, "video capture")                 debug.writeline("add capture filter our graph : " & dserror.geterrortext(hr))                 dserror.throwexceptionforhr(hr)                  hr = me.capturegraphbuilder.renderstream(pincategory.preview, mediatype.video, sourcefilter, nothing, nothing)                 debug.writeline("render preview pin on video capture filter : " & dserror.geterrortext(hr))                 dserror.throwexceptionforhr(hr)                  marshal.releasecomobject(sourcefilter)                  setupvideowindow()                  rot = new dsrotentry(me.graphbuilder)                  hr = me.mediacontrol.run()                 debug.writeline("start previewing video data : " & dserror.geterrortext(hr))                 dserror.throwexceptionforhr(hr)                  me.currentstate = playstate.running                 debug.writeline("the currentstate : " & me.currentstate.tostring)              catch ex exception                 messagebox.show("an unrecoverable error has occurred.with error : " & ex.tostring)             end try         end sub          private sub getinterfaces()             dim hr integer = 0             me.graphbuilder = ctype(new filtergraph, igraphbuilder)             me.capturegraphbuilder = ctype(new capturegraphbuilder2, icapturegraphbuilder2)             me.mediacontrol = ctype(me.graphbuilder, imediacontrol)             me.videowindow = ctype(me.graphbuilder, ivideowindow)             me.mediaeventex = ctype(me.graphbuilder, imediaeventex)             hr = me.mediaeventex.setnotifywindow(me.handle, wm_graphnotify, intptr.zero) 'this method designates window recipient of messages generated or sent current directshow object             dserror.throwexceptionforhr(hr) 'throwexceptionforhr wrapper marshal.throwexceptionforhr, additionally provides descriptions directshow specific error messages.if hr value not fatal error, no exception thrown:             debug.writeline("i started sub interfaces , result : " & dserror.geterrortext(hr))         end sub         public function findcapturedevice() ibasefilter             debug.writeline("start sub findcapturedevice")             dim hr integer = 0             dim classenum ienummoniker = nothing             dim moniker imoniker() = new imoniker(0) {}             dim source object = nothing             dim devenum icreatedevenum = ctype(new createdevenum, icreatedevenum)             hr = devenum.createclassenumerator(filtercategory.videoinputdevice, classenum, 0)             debug.writeline("create enumerator video capture devices : " & dserror.geterrortext(hr))             dserror.throwexceptionforhr(hr)             marshal.releasecomobject(devenum)             if classenum nothing                 throw new applicationexception("no video capture device detected.\r\n\r\n" & _                                "this sample requires video capture device, such usb webcam,\r\n" & _                                "to installed , working properly.  sample close.")             end if             if classenum.next(moniker.length, moniker, intptr.zero) = 0                 dim iid guid = gettype(ibasefilter).guid                 moniker(0).bindtoobject(nothing, nothing, iid, source)             else                 throw new applicationexception("unable access video capture device!")             end if             marshal.releasecomobject(moniker(0))             marshal.releasecomobject(classenum)             return ctype(source, ibasefilter)         end function         public sub setupvideowindow()             dim hr integer = 0             'set video window child of main window             'putowner : sets owning parent window video playback window.              hr = me.videowindow.put_owner(me.handle)             dserror.throwexceptionforhr(hr)              hr = me.videowindow.put_windowstyle(windowstyle.child or windowstyle.clipchildren)             dserror.throwexceptionforhr(hr)              'use helper function position video window in client rect of main application window             resizevideowindow()              'make video window visible, positioned             'put_visible : method changes visibility of video window.              hr = me.videowindow.put_visible(oabool.true)             dserror.throwexceptionforhr(hr)         end sub          protected overloads sub wndproc(byref m message)             select case m.msg                 case wm_graphnotify                     handlegraphevent()             end select             if not (me.videowindow nothing)                 me.videowindow.notifyownermessage(m.hwnd, m.msg, m.wparam.toint32, m.lparam.toint32)             end if             mybase.wndproc(m)         end sub         public sub handlegraphevent()             dim hr integer = 0             dim evcode eventcode             dim evparam1 integer             dim evparam2 integer             if me.mediaeventex nothing                 return             end if             while me.mediaeventex.getevent(evcode, evparam1, evparam2, 0) = 0                 '// free event parameters prevent memory leaks associated                 '// event parameter data.  while application not interested                 '// in received events, applications should process them.                 hr = me.mediaeventex.freeeventparams(evcode, evparam1, evparam2)                 dserror.throwexceptionforhr(hr)                  '// insert event processing code here, if desired             end while         end sub          protected overloads overrides sub dispose(byval disposing boolean)             if disposing                 '// stop capturing , release interfaces                 closeinterfaces()             end if             mybase.dispose(disposing)         end sub         public sub closeinterfaces()             '//stop previewing data             if not (me.mediacontrol nothing)                 me.mediacontrol.stopwhenready()             end if              me.currentstate = playstate.stopped              '//stop recieving events             if not (me.mediaeventex nothing)                 me.mediaeventex.setnotifywindow(intptr.zero, wm_graphnotify, intptr.zero)             end if              '// relinquish ownership (important!) of video window.             '// failing call put_owner can lead assert failures within             '// video renderer, still assumes has valid             '// parent window.             if not (me.videowindow nothing)                 me.videowindow.put_visible(oabool.false)                 me.videowindow.put_owner(intptr.zero)             end if              ' // remove filter graph running object table             if not (rot nothing)                 rot.dispose()                 rot = nothing             end if              '// release directshow interfaces             marshal.releasecomobject(me.mediacontrol) : me.mediacontrol = nothing             marshal.releasecomobject(me.mediaeventex) : me.mediaeventex = nothing             marshal.releasecomobject(me.videowindow) : me.videowindow = nothing             marshal.releasecomobject(me.graphbuilder) : me.graphbuilder = nothing             marshal.releasecomobject(me.capturegraphbuilder) : me.capturegraphbuilder = nothing          end sub          private sub form1_resize1(byval sender object, byval e system.eventargs) handles me.resize             if me.windowstate = formwindowstate.minimized                 changepreviewstate(false)             end if             if me.windowstate = formwindowstate.normal                 changepreviewstate(true)             end if             resizevideowindow()         end sub         public sub changepreviewstate(byval showvideo boolean)             dim hr integer = 0             '// if media control interface isn't ready, don't call             if me.mediacontrol nothing                 debug.writeline("mediacontrol nothing")                 return             end if             if showvideo = true                 if not (me.currentstate = playstate.running)                     debug.writeline("start previewing video data")                     hr = me.mediacontrol.run                     me.currentstate = playstate.running                 end if             else                 debug.writeline("stop previewing video data")                 hr = me.mediacontrol.stopwhenready                 me.currentstate = playstate.stopped             end if         end sub         public sub resizevideowindow()             'resize video preview window match owner window size             'left , top , width , height             if not (me.videowindow nothing) 'if videopreview not nothing                 me.videowindow.setwindowposition(0, 0, me.width, me.clientsize.height)             end if         end sub     end class end namespace 

project link - http://www.codeproject.com/articles/18511/webcam-using-directshow-net


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -