Saving Snapshots to PNG in Silverlight 4 and the WebCam | John Papa

John Papa

Evangelist on the loose

Saving Snapshots to PNG in Silverlight 4 and the WebCam

...

This week I decided to experiment with a few webcam features and put together an application that activates a webcam, allows the user to take snapshots, and save the snapshots to PNG files. I like when demos use MVVM out of the box instead of just throwing all of the code in a single code behind, so one of my goals was to make a passing effort to break the code out and use MVVM practices.

The first thing I did was grab Tim Heuer’s webcam sample which already handles activating the webcam. Tim did a great job with this, so instead of reinventing the wheel for this demo I decided to start with his code and add my requirements. First I wanted to hit the UI, so I created a new project, and copied the UI from Tim then took out what I did not need and add what I did need. Of course I did all of this in Expression Blend. Cider is great, but I’m faster in Blend by far. The UI is very simple, so this was a minimal effort. Here is the basic UI:

image

This sample application uses the .NET ImageTools library. You can grab the source or binaries for it on codeplex here.

Files

There are only a few files in this project, as shown in the image below.

  • MainPage.xaml
    • View
  • MainPageViewModel
    • ViewModel
  • DelegateCommand
    • Generic class to handle the commanding features
  • ViewModelBase
    • Contains basic VM features, like INotifyPropertyChanged implementation
  • BitmapExtensions
    • Contains some simplification code to save to PNG from a WriteableBitmap
    • This uses the ImageTools.dll from codeplex

image

ViewModel

The demo uses a View named MainPage.xaml as shown in the above screen capture. The View is bound to an instance of a ViewModel named MainPageViewModel. The VM contains several properties that the View uses in its bnidings:

  • ICommand StartCaptureCommand
  • ICommand StopCaptureCommand
  • ICommand TakeSnapshotCommand
  • ICommand SaveSnapshotCommand
  • ObservableCollection<WriteableBitmap> Images
  • VideoBrush VideoBrush
  • WriteableBitmap SelectedSnapshot

The commands handle the interaction with the capture process and saving to the PNG file. We’ll look at those closer in a moment. The Images collection contains a series of WriteableBitmap instances that are gathered from the capture process. When someone clicks the Take Snapshot button, the WriteableBitmap is retrieved from the capture stream and added to the Images collection.

The VideoBrush property exposes the video stream to the View, through bindings. I am not convinced this is the ideal way to go as I generally do not like to expose a brush from a VM since a brush is View specific. One alternative is to expose the stream in another fashion and use a converter (yuk). Or another option is to perform the video capture in the View. I don;t have a problem with this being done in the View, but I wanted to show how this could work with MVVM. In this case I feel it’s acceptable either way.

The SelectedSnapshot property represents the WriteableBitmap that the user selects in the UI. When the user clicks the Save Snapshot button, this property is checked and saved to a PNG file.

CaptureSource

The code to activate the webcam and begin capturing the video stream is straightforward. First I created a private property named CapSource in my ViewModel. The CapSource is set in the constructor of the VM, with the video device set to the default video capture device as shown below:

   1: CapSource = new CaptureSource

   2: {

   3:     VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice()

   4: };

Commanding

I hooked a Command up to the “Start Capture” button using the new commanding features in Silverlight 4. The XAML to do this is very simple … you just bind to the Command which is in the ViewModel, in this case.

   1: <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">

   2:     <Button Margin="5" Content="Start Capture" Height="25" Command="{Binding StartCaptureCommand}" />

   3:     <Button Margin="5" Content="Stop Capture" Height="25" Command="{Binding StopCaptureCommand}" />

   4:     <Button Margin="5" Content="Take Snapshot" Height="25" Command="{Binding TakeSnapshotCommand}" />

   5:     <Button Margin="5" Content="Save Snapshot" Height="25" Command="{Binding SaveSnapshotCommand}"/>

   6: </StackPanel>

The commands are set up in the constructor of the VM. I assigned each of the commands an action (for performing the action when the button is clicked) and a function (for determining if the button should be enabled or not). In Silverlight 4 we have the ICommand interface and now the new binding properties: Command and CommandProperty. We still need a commanding implementation. You can write one for each command (yuk), create some sort of command manager, or use an implementation of the DelegateCommand class. I chose the latter in this case because it is simple and still abstracts the boring parts of the commands for reuse. (You can check out the DelegateCommand class in the source code for this sample project.)

   1: StartCaptureCommand = new DelegateCommand(StartCapture, CanStartCapture);

   2: StopCaptureCommand = new DelegateCommand(StopCapture, CanStopCapture);

   3: TakeSnapshotCommand = new DelegateCommand(TakeSnapshot, CanTakeSnapshot);

   4: SaveSnapshotCommand = new DelegateCommand(SaveSnapshot, CanSaveSnapshot);

Capture Video

When the user clicks the button, the command fires in the VM. The StartCapture method checks the state of the capture and stops it if it is already in process. Then the VideoBrush is set to the capture source.

   1: private void StartCapture(object obj)

   2: {

   3:     if (CapSource != null)

   4:     {

   5:         if (CapSource.State != CaptureState.Stopped)

   6:         {

   7:             CapSource.Stop(); // stop whatever device may be capturing

   8:         }

   9:  

  10:         // create the brush

  11:         VideoBrush.SetSource(CapSource);

  12:  

  13:         // request user permission and display the capture

  14:         if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())

  15:         {

  16:             CapSource.Start();

  17:         }

  18:     }

  19:     RefreshCommandStates();

  20: }

The “if” statement checks if the user has already allowed access to the device by checking CaptureDeviceConfiguration.AllowedDeviceAccess. If not, the user will be prompted to allow access to the camera and microphone using the CaptureDeviceConfiguration.RequestDeviceAccess method (see below). Then the capture process is started.

image

Finally, the RefreshCommandStates method is fired, which indirectly (through INPC events and bindings) enables or disables the buttons appropriately.

Take a Snapshot from the Video

When the capture process has started and a user clicks the Take Snapshot button, the appropriate command is invoked in the VM and the capture source’s AsyncCaptureImage method is invoked. I used the Dispatcher to make sure I operated on the UI thread because I wanted to not only grab the bitmap but I also want to update the View’s state. Again, not sure I like this in a VM, but I’ll take some liberties here and apply the ForemostItHasToWork pattern.

   1: private void TakeSnapshot(object obj)

   2: {

   3:     if (CapSource != null && CapSource.State == CaptureState.Started)

   4:     {

   5:         CapSource.AsyncCaptureImage((bitmap) =>

   6:             {

   7:                 App.Current.Resources.Dispatcher.BeginInvoke(() =>

   8:                 {

   9:                     Images.Add(bitmap);

  10:                     SelectedSnapshot = bitmap;

  11:                     RefreshCommandStates();

  12:                 });

  13:             });

  14:     }

  15: }

 image

Save to PNG

Once the user has selected an image in the list, the Save Snapshot button is enabled. When the user clicks this button the SaveSnapshotCommand in the VM retrieves the WriteableBitmap from the SelectedSnapshot  property (which is bound to the SelectedItem of the ListBox). Then an extension method I created on the WriteableBitmap saves the snapshot to a PNG file.

   1: private void SaveSnapshot(object obj)

   2: {

   3:     var bitmap = SelectedSnapshot;

   4:     if (bitmap != null)

   5:     {

   6:         bitmap.SaveToPNG();

   7:     }

   8:     RefreshCommandStates();

   9: }

This prompts the user for the file name and saves the png.

image

A Word About PNG Conversion

Once you have a WriteableBitmap (or an image for that matter) you can convert it to a PNG file. To do this it makes sense to use one of the existing libraries of conversion tools. One such tool was written by Joe Stegman and can be found here. Joe’s PNG converter is straightforward and simply requires that you add 2 classes to your project. Another option is to use one of the tools on codeplex. A popular choice seems to be the .NET ImageTools library (thanks to several people on Twitter for alerting me to this). This library can be referenced and used pretty easily. It does more than just conversion to a PNG, but it handles what I need with relative ease. For all it does, I could not find a simple method that accepted a WriteableBitmap and outputted a PNG (or any file format). So I quickly wrote up quick extension method  that handles this for me. Since this was my first glance at this open source API, it is likely that there is an easier/better way to convert from a WriteableBitmap to a PNG using ImageTools, so if you find one please leave a comment and I will update the code. It does the job nicely though and handles compression of the PNG.

   1: public static void SaveToPNG(this WriteableBitmap bitmap)

   2: {

   3:     if (bitmap != null)

   4:     {

   5:         SaveFileDialog sfd = new SaveFileDialog

   6:                  {

   7:                  Filter = "PNG Files (*.png)|*.png|All Files (*.*)|*.*",

   8:                  DefaultExt = ".png",

   9:                  FilterIndex = 1

  10:                  };

  11:  

  12:         if ((bool)sfd.ShowDialog())

  13:         {

  14:             var img = bitmap.ToImage();

  15:             var encoder = new PngEncoder();

  16:             using (Stream stream = sfd.OpenFile())

  17:             {

  18:                 encoder.Encode(img, stream);

  19:                 stream.Close();

  20:             }

  21:         }

  22:     }

  23: }

From Here

The webcam and microphone features in Silverlight 4 are fun to work with and can be useful to grab images and save them (or email them, or stream them, etc). I had a good time putting this together and learned about the PNG conversion options along the way.

You can download the source code here for the entire sample application. Enjoy!

UPDATE (Jan 03,2010): Laurent Bugnion alerted me that there indeed is a ToImage method in the ImageTools library. The trick is to make sure you download the right link from the ImageTools codeplex site. The first link just gives you the basics, the second link gives you all the dll’s. Once I downloaded that second link I was able to add references to a few assemblies in the ImageTools library that cleaned out some of my code for my extension method. The net result is that my extension method is considerably shorter and clearer. I just updated the extension method in the above code window and in the downloadable code. Feel free to grab the latest and run with it!

tags: Silverlight
  • Anonymous

    Using ImageTools, you could try the ToImage extension:
    Image image = myWriteableBitmap.ToImage()

  • http://johnpapa.net JohnPapa

    ecb – I don’t see en extension for ToImage in on a WriteableBitmap in ImageTools.dll.

  • Anonymous

    How can this error be fixed???
    Thanks
    Error 1 The "ValidateXaml" task failed unexpectedly.
    System.IO.FileLoadException: Could not load file or assembly ‘file:///C:\Users\rapuke\Desktop\Projects\Silverlight\Webcam with Save\Capture02\Capture02\Lib\ImageTools.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0×80131515)
    File name: ‘file:///C:\Users\rapuke\Desktop\Projects\Silverlight\Webcam with Save\Capture02\Capture02\Lib\ImageTools.dll’ —> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See go.microsoft.com/…/fwlink for more information.
    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
    at System.Reflection.Assembly.LoadFrom(String assemblyFile)
    at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
    at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
    at Microsoft.Silverlight.Build.Tasks.ValidateXaml.Execute()
    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)
    Capture02

  • http://johnpapa.net JohnPapa

    dcw – The reason you get that error is usually because the downloaded file needs to be unblocked first. Go to the dll in windows explorer, right click it and unblock it. This has happened to me a few times.

  • http://dotnetshoutout.com/Saving-Snapshots-to-PNG-in-Silverlight-4-and-the-WebCam-JohnPapa Anonymous

    Thank you for submitting this cool story – Trackback from DotNetShoutout

  • http://blog.galasoft.ch Anonymous

    Hey John,
    To use the ImageTools ToImage extension method, you must explicitly include a using statement:
    using ImageTools;
    You also need to add a reference to the assemblies ImageTools, ImageTools.IO.Png and ImageTools.Utils. If you want another format, then you must of course use the corresponding assembly.
    HTH,
    Laurent

  • http://blog.cwa.me.uk/2010/01/04/the-morning-brew-509/ Anonymous

    Pingback from The Morning Brew – Chris Alcock » The Morning Brew #509

  • http://johnpapa.net JohnPapa

    Laurent – Thanks. I updated my post (in red) to reflect the changes you suggested. Turns out there are 2 different downloads on the ImageTools codeplex site … not intuitive. But in any event, it is a very helpful utility, once you download the right one :)

  • http://www.orcsweb.com/blog/brad/saving-snapshots-to-png-in-silverlight-4-and-the-webcam/ Anonymous

    John Papa has a cool blog post about capturing images from a webcam using Silverlight. Check it out.

  • http://www.planszowki.com Anonymous

    This is great post. I just start to learn Silverlight and tutorials like this are very helpful fur such a noob like me;) Thanks!

  • patrick_eman

    A beautiful day!
    i have one question ,
    I’m currently learning silverlight 4.0 running in OUT-OF-Browser Application
    I have a problem in saving images to a specific file path.
    can you help me

  • patrick eman

    i have a problem on saving my images to a specific folder in my hard drive i’m already using silverlight 4.0 OOB application
    can you please tell me how to save my image.

  • Neha sharma

    Hey,
    I need to save the captured image to the server folder.. without prompting Save File Dialog… Can u plz help me how to do this….

%d bloggers like this: