Click here to Skip to main content
15,918,275 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hello everyone,

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
Posted
Updated 6-Apr-11 15:58pm
v2
Comments
walterhevedeich 6-Apr-11 21:40pm    
This is a long post. Only few will have the patience to view this question. Update your post to contain only the relevant parts.
tofa111 6-Apr-11 22:06pm    
i modified it thank you

I having exactly same problem,
I am still researching, will post the solution once i find it
 
Share this answer
 
Comments
tofa111 17-May-11 1:10am    
you don't need to do this, there is a better library I found.
here the link:

http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download[^]

you just need to download the files in the bottom of the page then add the DLL to your solution and see the example in the other file to know how to use, believe me it's the easiest library I found
good luck
you don't need to do this, there is a better library I found.
here the link:

http://sites.google.com/site/webcamlibrarydotnet/winfrom-and-csharp-sample-code-and-download[^]

you just need to download the files in the bottom of the page then add the DLL to your solution and see the example in the other file to know how to use, believe me it's the easiest library I found
good luck
 
Share this answer
 
that's pretty simple and easy to use.
unfortunately, I am stuck with DirectShow,
I can use the webcam component for the second viewer, but the main viewer has to use DS.
 
Share this answer
 
In CloseInterfases method, add the next Lines:
C#
if (graphBuilder != null)
 {
     graphBuilder.RemoveFilter(capFilter);
 }
 baseGrabFlt = null;
 if (sampGrabber != null)
     Marshal.ReleaseComObject(sampGrabber); sampGrabber = null;

 if (capGraph != null)
     Marshal.ReleaseComObject(capGraph); capGraph = null;

 if (graphBuilder != null)
     Marshal.ReleaseComObject(graphBuilder); graphBuilder = null;

 if (capFilter != null)
     Marshal.ReleaseComObject(capFilter); capFilter = null;

 if (capDevices != null)
 {
     foreach (DsDevice d in capDevices)
         d.Dispose();
     capDevices = null;
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900