mostafa
mostafa

Reputation: 11

directshow problem

I'm using DirectShow.net to capture images from a webCame,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 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

Upvotes: 1

Views: 2006

Answers (3)

Lucindo Mora
Lucindo Mora

Reputation: 51

Yes, in the CloseInterfases() method, add the following lines:

if (graphBuilder != null)
{
    graphBuilder.RemoveFilter(capFilter);
}
baseGrabFlt = null;

Upvotes: 0

patola
patola

Reputation: 81

The problem is probably when you are closing the interfaces, you have to use Marshal.ReleaseComObject in all your interfaces of DirectShow, and also, you have to use the method RemoveFilter of your IGraphBuilder to relase your used capture filter, if you don't do so the graph builder won't be released.

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160892

Only one graph for a single video source can be running at a time, think of it as a pipeline with a single source.

If you want to start another graph, you will have to stop the first one first - your code currently does that in CloseInterfaces(); - so you should be fine if you call that before building up the second graph.

Upvotes: 1

Related Questions