Search code examples
c#visual-studiowebcamdirectshow

DirectShowLib - Can't take more than one picture with webcam. Got "Insufficient system resources exist to complete the requested service" error


I'm working on Visual Studio 2012 and using C#, I'm using the library DirectShowLib to take pictures from my webcam, but I can only take one picture, if I try to take another one I get an exception:

   DirectShowLib.DsError.ThrowExceptionForHR(Int32 hr)
   at SnapShot.Capture.SetupGraph(DsDevice dev, Int32 iWidth, Int32 iHeight, Int16 iBPP, Control hControl) in c:\Users\devel_000\Documents\Visual Studio 2012\Projects\ControlAcceso\ControlAcceso\Capture.cs:line 323
   at SnapShot.Capture..ctor(Int32 iDeviceNum, Int32 iWidth, Int32 iHeight, Int16 iBPP, Control hControl) in c:\Users\devel_000\Documents\Visual Studio 2012\Projects\ControlAcceso\ControlAcceso\Capture.cs:line 86
   at ControlAcceso.PhotoWindow..ctor() in c:\Users\devel_000\Documents\Visual Studio 2012\Projects\ControlAcceso\ControlAcceso\PhotoWindow.cs:line 32
   at ControlAcceso.CheckWindow.pbPhoto_Click(Object sender, EventArgs e) in c:\Users\devel_000\Documents\Visual Studio 2012\Projects\ControlAcceso\ControlAcceso\CheckWindow.cs:line 108
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at ControlAcceso.Program.Main() in c:\Users\devel_000\Documents\Visual Studio 2012\Projects\ControlAcceso\ControlAcceso\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

I'm using the DxSnap example [link] to take the pictures. the exception is thrown in the method SetupGraph [link]

The line DirectShowLib.DsError.ThrowExceptionForHR(Int32 hr) caught my attention, what does it mean? How can I fix this?


Solution

  • The error -2147023446 is 0x800705AA, ERROR_NO_SYSTEM_RESOURCES "Insufficient system resources exist to complete the requested service." (see this post and tool on reading HRESULT values easily and in convenient way).

    I would say most likely you are trying to open second pipeline without closing first. And video capture devices are opened exclusively, hence you cannot start a pipeline if there is another one still active. The error code is suggesting exactly this.

    In order to work this around, you need to make sure you call IMediaControl.Stop on previous filter graph, and release COM interfaces you don't need anymore with Marshal.ReleaseComObject.