how to restart the stream using directshow

By mostafa mahmud

web cam in directshow

hello everyone, I'm using DirectShow.net to capture images from a webCam, after searching the web I got this code and it works fine: to see the original full code kindly follow the link below

bool SetupGraph()
        {
             int hr;
            try
            {
                hr = capGraph.SetFiltergraph(graphBuilder);
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);

                hr = graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device");
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);

                DsUtils.ShowCapPinDialog(capGraph, capFilter, this.Handle);

                AMMediaType media = new AMMediaType();
                media.majorType = MediaType.Video;
                media.subType = MediaSubType.RGB24;
                media.formatType = FormatType.VideoInfo; // ???
                hr = sampGrabber.SetMediaType(media);
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);

                hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);

               Guid cat = PinCategory.Preview;
               Guid med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null); // baseGrabFlt
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);

                cat = PinCategory.Capture;
                med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, baseGrabFlt); // baseGrabFlt
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);

                media = new AMMediaType();
                hr = sampGrabber.GetConnectedMediaType(media);
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);
                 if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
                     throw new NotSupportedException("صيغه غير معروفه");

                videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
               Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero;

                hr = sampGrabber.SetBufferSamples(false);
                 if (hr == 0)
                    hr = sampGrabber.SetOneShot(false);
                 if (hr == 0)
                    hr = sampGrabber.SetCallback(null, 0);
                 if (hr < 0)
                   Marshal.ThrowExceptionForHR(hr);

                 return true;
            }
            catch (Exception ee)
             {
                 MessageBox.Show(this, "Could not setup graph\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                 return false;
            }
        }

my problem is when I call this form from another form for a second time it gives me this error: "Value does not fall within the expected range" when the compiler comes to this section:
hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null); actually i discovered that the problem disappear when I unplug the web-came physically and plug it again, so I concluded that i need to unplug it using code so please if u know tell me how to do it or if u have any better idea like how to restart the stream please tell me you can access the original code on http://www.codeproject.com/script/Content/ViewAssociatedFile.aspx?rzp=%2FKB%2Fdotnet%2FROTEsys%2Frotesys_src.zip&zep=Tracking.cs&obid=9401&obtid=2&ovid=1[^]

thank you for ur time and patience

how to restart the stream using directshow  (1837 Views)