Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
I am going to talk about web cameras in Silverlight support. Stay tuned! I am in a project called WorkingWithwebCam. It's a very simple interface. I am in the MainPage.xaml file. I have two buttons: I have the Start webCam button and the Stop button. Plus I also have a rectangle in this area of the screen called webcamOutputRectangle. What I am going to do is capture the video from the camera and place it in this rectangle with a video brush. Let me show you the code for doing that. Press F7 look at the code behind.
In that loaded event on this page I am creating a brand-new capture source. Then I am setting up two event procedures: one that fires when the capture fails, and the other fires when the capture image is complete. More important is this line of code here. This goes to the operating system that says GetDefaultVideoCaptureDevice on this computer. When I make that call, I will check to see if I did get a camera device back. It's possible there are no cameras installed in the user's computer. And once I get it, I'm going to do a little bit of code to set the video capture device to that web cam, generate a video brush, and then set the source to the camera, and then finally take their web cam brush and apply it to the output rectangle I showed you.
So that will start putting the pixels in that rectangle. Now I just need to start the camera and stop the camera. That's really easy to do. I go out to the computer and I ask for access to computer's camera. The user will be prompted with a dialog that asks if it's safe to turn on the camera. If they allow that access, then this code will run. And all I do in here is a captureSource.Start. I think it's pretty simple code. And I do almost the same thing when I am stopping that video and check to make sure I have a reference; If I do then I call Stop.
Let's see what it looks like. Press F5 and the application will run. I am going to click on the Start webCam. Here is the dialog I mentioned. Notice that there's a Remember my answer checkbox here? If the user clicks Remember my answer and then chooses Yes thennext time I ask for camera access, it will automatically be granted. There I am. Hi! This is my booth here at lynda.com. I will just click Stop.
Notice that it takes the last frame and keeps that painted inside the rectangle. I think we can probably do some more interesting applications than just a video stream like that. And to show you an example, I am going to use something from the coding for fun web site called SilverShader. I am going to go head and say File > Open > Project. I'm in the same folder as the working with web cam. I am going to open up the SilverShader folder and then SilverShader again and then Solution and then this SLN file.
I am going to start up the application, and what this does is it takes the video stream and runs it through a pixel shader first before showing it on the screen. So I am going to come down here and click on the Capture button. Approve the access. And here I am again, but this time it's applying a circular shader to the output. So I can change the circle Sizes, Circle Min and Max. I can choose from other. I don't like this edge detection which has kind of a scary look to it.
I can also pick this one down here. It's called Old Movie. It kind of looks like an old tint type movie there. I could control the number of scratches on the screen. I can also control the amount of noise its showing. There is a few other for you to check out in this demo. I highly encourage you to look at running source code. There is some very interesting stuff in this example. Let me stop it here and then return back to Visual Studio. Uploading videos to your server is a common request. One idea a lot of developers have when they first see web cam support in Silverlight is that you could build some sort of video chat program.
That's difficult to do in Silverlight currently due to codec issues. The basic problem is that you can capture the video but you have no built-in way of encoding or compressing it before sending it to the server. As you can imagine, uploading a massive AV file is not a test you want to tackle lightly. Microsoft and other parties are working in this area, but there's nothing ready yet.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 103238 Viewers
61 Video lessons · 89973 Viewers
71 Video lessons · 73553 Viewers
56 Video lessons · 105091 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.