Silverlight-Catch video and Save Screenshot

In silverlight, we can catch video equipment to create video session system, or upload avatar through screenshot. In this post, I will introduce how to visit video equipment and download screentshot files to computer.

1. check if the default camera and microphone is available in silverlight.

2. Then, copy the following xaml code.

    <Grid x:Name=“LayoutRoot” Background=“White”>

    <Border BorderBrush=“Silver” BorderThickness=“1” Height=“346” HorizontalAlignment=“Left Margin=”21,19,0,0” Name=”border1” VerticalAlignment=”Top” Width=”477” >


    <VideoBrush x:Name=“ShowVideo”></VideoBrush>



        <Button Content=“Open Camera” Height=“32” HorizontalAlignment=“Left” Margin=“38,380,0,0” Name=“button1” VerticalAlignment=“Top” Width=“95” Click=“button1_Click” />

        <Button Content=“Close Camera” Height=“32” HorizontalAlignment=“Left” Name=“button2” Width=“85” VerticalAlignment=“Top” Margin=“268,380,0,0” Click=“button2_Click” />

        <Button Content=“Screenshot” Height=“32” Name=“button3” Margin=“153,380,0,0” HorizontalAlignment=“Left” Width=“91” VerticalAlignment=“Top” Click=“button3_Click” />

        <StackPanel Height=“346” HorizontalAlignment=“Left” Margin=“514,19,0,0” Name=“stackPanel1” VerticalAlignment=“Top” Width=“460” />


We create a Border to display video image and add three buttons to control to open, close camera and printscreen. Then, add StackPanel to display the screenshot.

3. copy the following CS code.

    public partial class MainPage : UserControl


        public MainPage()




        //Provide Methods for Audio and Video

        CaptureSource video = new CaptureSource();

        private void button1_Click(object sender, RoutedEventArgs e)


            //Get Default Video Object on Computer

            VideoCaptureDevice camera = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

            //Succeed to Access to Video Equipment on Computer

            if (CaptureDeviceConfiguration.RequestDeviceAccess())


                //Set Video Equipment as Camera

                video.VideoCaptureDevice = camera;

                //VideoBrush Set Source as video


                ShowVideo.Stretch = Stretch.Fill;

                //Catch Video




        private void button3_Click(object sender, RoutedEventArgs e)



            WriteableBitmap wBitmap = new WriteableBitmap(border1, new MatrixTransform());

            Image img = new Image();

            img.Width = 450;

            img.Margin = new Thickness(2);

            img.Source = wBitmap;

            //Save Image

            if (wBitmap != null)


                SaveFileDialog saveDlg = new SaveFileDialog();

                saveDlg.Filter = “JPEG Files (*.jpeg)|*.jpeg”;

                saveDlg.DefaultExt = “.jpeg”;

                if ((bool)saveDlg.ShowDialog())


                    using (Stream fs = saveDlg.OpenFile())


                        SaveToFile(wBitmap, fs);

                        MessageBox.Show(“Succeed to Save”);








        ///Save Image


        ///<param name=”bitmap”></param>

        ///<param name=”fs”></param>

        private static void SaveToFile(WriteableBitmap bitmap, Stream fs)


            int width = bitmap.PixelWidth;

            int height = bitmap.PixelHeight;

            int bands = 3;

            byte[][,] raster = new byte[bands][,];

            for (int i = 0; i < bands; i++)


                raster[i] = new byte[width, height];


            for (int row = 0; row < height; row++)


                for (int column = 0; column < width; column++)


                    int pixel = bitmap.Pixels[width * row + column];

                    raster[0][column, row] = (byte)(pixel >> 16);

                    raster[1][column, row] = (byte)(pixel >> 8);

                    raster[2][column, row] = (byte)pixel;



            FluxJpeg.Core.ColorModel model = new FluxJpeg.Core.ColorModel

            { colorspace = FluxJpeg.Core.ColorSpace.RGB };

            FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);

            //Encode the Image as a JPEG

            MemoryStream stream = new MemoryStream();

            FluxJpeg.Core.Encoder.JpegEncoder encoder =

                new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);


            //Back to the start

            stream.Seek(0, SeekOrigin.Begin);

            //Get teh Bytes and write them to the stream

            byte[] binaryData = new Byte[stream.Length];

            long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);

            fs.Write(binaryData, 0, binaryData.Length);


        private void button2_Click(object sender, RoutedEventArgs e)


            //Stop Video




Two Components Recommend:

Spire.Doc, is used to operate MS Word document for .Net and Silverlight, including basic manipulations (generate, open, edit documents), mail merge and other Word functions manipulations.

Spire.XLS, is used to operate MS Excel for .NET and Silverlight, including basic manipulations (generate, open, edit files), chart creation and data exporting.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s