C # webcam User Control Source

Source: Internet
Author: User

Some people have asked if they cocould take a look at the source for the webcam Vista sidebar gadget. after a little bit of cleaning up, I'm posting it now for you to take a look. here are some things worth mentioning:

1. uses the DirectShow. Net Library

2. I found some source in VB. NET and used that as a baseline (stored med the conversion and cleaned up stuff that really wasn't needed)

3. I am in no way a DirectX/DirectShow expert. any questions sent my way will likely result in a blank stare back at you (though FWIW, I do pick up things quickly and might be able to at least get you started on the correct path)

4. If you look at the source, you might think "I thought this was a vista sidebar gadget. Where is the gadget source? "That's the easy part. at the bottom of this post is a link to my. net gadget creator application that I wrote (with instructions on how to use it with the webcam control ). that application will allow you to take any. net usercontrol and convert it into a vista sidebar gadget. just compile the webcamcontrol2 control, and use. net gadget creator to create an instant Vista sidebar gadget.

5. you may receive an error when you run the provided test application saying something along the lines of "invalid argument ". this is a known issue and is happens when more than one application tries to access the same camera. I haven't looked into fixing this so if someone wants to take a stab at it, clue me in on how to fix it.

When looking through the source (there isn't that much to it) Pay attention to the 2 primary methods. The first isGetinterfacesMethod which creates all the necessary DirectShow interfaces and then creates the connection between DirectShow and your usercontrol window (events are passed to the control via window messages ).

TheFindcapturedeviceMethod enumerates through your devices looking for the 1st video device it finds that provides an input (Filtercategory. inputdevice). This is done by creating a device class enumerator. currently, the source will just grab the first device that it sees. if anyone is interested in knowing how to present the user with a list of all input devices, I can write some code to do that as well. just let me know.

103 ucomienummoniker classenum = NULL;

104 ucomimoniker [] moniker = new ucomimoniker [1];

105 object origin = NULL;

106

107 icreatedevenum devenum = (icreatedevenum) (new createdevenum ());

108 int hR = devenum. createclassenumerator (filtercategory. videoinputdevice, outclassenum, cdef. None );

109 dserror. throwexceptionforhr (HR );

110 marshal. releasecomobject (devenum );

Once the class enumerator is created, I just grab the first one that was found (assuming one was found) and bind it to an ibasefilter object which I then return.

117 int none = 0;

118

119 If (classenum. Next (moniker. length, moniker, out none) = 0)

120 {

121 guid IID = typeof (ibasefilter). guid;

122 moniker [0]. bindtoobject (null, null, ref IID, out source );

123}

124 else

125 {

126 throw new applicationexception ("unable to access video capture device! ");

127}

128

129 marshal. releasecomobject (moniker [0]);

130 marshal. releasecomobject (classenum );

131

132 return (ibasefilter) source;

Once the video input device is found and we have our base filter, we associate it with an icapturegraphbuilder2 object by telling it render a video stream through the preview pin of the input device (line 68 below of the capturevideo method ).

51 private void capturevideo ()

52 {

53 int hR = 0;

54 ibasefilter sourcefilter = NULL;

55 try

56 {

57 // create the necessary DirectShow Interfaces

58 getinterfaces ();

59

60 hR = This. capturegraphbuilder. setfiltergraph (this. graphbuilder );

61 dserror. throwexceptionforhr (HR );

62

63 sourcefilter = findcapturedevice ();

64

65 hR = This. graphbuilder. addfilter (sourcefilter, "webcamcontrol video ");

66 dserror. throwexceptionforhr (HR );

67

68 hR = This. capturegraphbuilder. renderstream (pincategory. Preview, mediatype. Video, sourcefilter, null, null );

69 Debug. writeline (dserror. geterrortext (HR ));

70 dserror. throwexceptionforhr (HR );

71

72 marshal. releasecomobject (sourcefilter );

73

74 setupvideowindow ();

75

76 hR = This. mediacontrol. Run ();

77 dserror. throwexceptionforhr (HR );

78

79 This. currentstate = playstate. running;

80}

81 catch (exception ex)

82 {

83 MessageBox. Show ("an unrecoverable error has occurred. \ r \ n" + ex. tostring ());

84}

85}

Now, if you want to use that control in the Vista sidebar, just download and install the following application:

When you launch the application and click Next it will ask you to add. net assemblies. click the Add button, navigate to the webcamtest or webcamcontrol2 bin directory and select the webcamcontrol2.dll and the directshowlib. DLL assemblies as shown below.

Click Next twice to get to the "select usercontrol to embed ". select the webcamcontrol2.webcamcontrol2 type and click Next. the next screen allows you to enter the information about the gadget. I provided an icon for you in the webcamtest base directory. after you fill in the information, keep clicking Next and the gadget will be built for you. go into the output directory you specified and you'll see. gadget file. simply double click on this and if all goes well, you should be looking at your webcam in the Vista sidebar. also, since you used. net gadget creator application, your gadget will uninstall successfully even while it is running (see. net sidebar gadget creator update #2 for more information ).

Without further ado, here's the link to the webcamcontrol source code.

Link: http://blogs.msdn.com/ B /markhsch/archive/2007/11/19/c-webcam-user-control-source.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.