开发者

directshow problem

开发者 https://www.devze.com 2023-02-22 09:49 出处:网络
I\'m using DirectShow.net to capture images from a webCame,after searching the web I got this code and it works fine:

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); // baseGrab开发者_开发百科Flt 
                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


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.


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消