Sie sind auf Seite 1von 3

C# Corner

Capturing and Saving Video in Windows 10 Application


Aditya Bhardwaj Sep 10 2015 Blog

0 1 3.5k

Download Free Office API

Video capturing is one of the common tool used now a days in application development. It is quite obvious a person might be needing to
integrate Video capturing in his Windows 10 Application. So, here we are beginning with our Video Capturing application:

In Visual Studio 2015, Start a new project and select Blank app in Universal App category. Before starting any coding, we have to go to
package.appxmanifest file of the solution, we need to select Webcam and Video Library under Capabilities.

Then in the MainPage.xaml file we will add the code for button which will be used to capture the video and a media element where video will
be shown:

01. <Button x:Name="Capture_button" Content="Start Capturing Video" HorizontalAlignment="Left" Margin="79,240,0,0" VerticalAlignmen


02. <MediaElement x:Name="video" HorizontalAlignment="Left" Height="720" Margin="312,0,0,0" VerticalAlignment="Top" Width="968"
03. <Button x:Name="Save_button" Content="Save Video" HorizontalAlignment="Left" Margin="79,391,0,0" VerticalAlignment="Top"

After adding the above code in the xaml file, we will go to the methods of the buttons: Capture_button and Save_button and add their
functionality. But before adding the functionality to the methods, we need to add the namespaces which will be required in the application:

01. using Windows.Media.Capture;


02. using Windows.Media.Core;
03. using Windows.Media.Editing;
04. using Windows.Storage;
05. using Windows.Storage.Pickers;
06. using Windows.Storage.Streams;
07. using Windows.UI.Popups;
08. using Windows.UI.Xaml;
09. using Windows.UI.Xaml.Controls;

Now, we add the declaration code for two objects of StorageFile class and IRandomAccessStream class which will be used throughout the
application:

01. private StorageFile sf;


02. private IRandomAccessStream rs;

Now, the functionality for Capture_button method which will invoke the default video capturing tool of the system:

01. private async void Capture_button_Click(object sender, RoutedEventArgs e)


02. {
03. // Declaring CameraCaptureUI to call the default Video capturing tool in the system
04. CameraCaptureUI cc = new CameraCaptureUI();
05. cc.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;
06. cc.VideoSettings.MaxResolution = CameraCaptureUIMaxVideoResolution.HighDefinition;
07.
08. /* saving the video temporarily in the storage file object
09. and setting the streaming source for our media element to show the information*/
10. sf = await cc.CaptureFileAsync(CameraCaptureUIMode.Video);
11. if (sf != null)
12. {
13. rs = await sf.OpenAsync(FileAccessMode.Read);
14. MediaClip mc = await MediaClip.CreateFromFileAsync(sf);
15. MediaComposition mcomp = new MediaComposition();
16. mcomp.Clips.Add(mc);
17. MediaStreamSource mss = mcomp.GeneratePreviewMediaStreamSource((int) video.ActualWidth, (int)video.ActualHeight);
18. video.SetMediaStreamSource(mss);
19. }
20. }

Now, as the save functionality we used in the last blog of Capture and save Image, same we will be using here but with minor adjustments
for the Video file extensions:
01. private async void save_Video(object sender, RoutedEventArgs e)
02. {
03. // Adding try catch block in case of occurence of an exception
04. try
05. {
06. // Creating object of FileSavePicker and adding few values to the properties of the object.
07. FileSavePicker fs = new FileSavePicker();
08. fs.FileTypeChoices.Add("Video", new List<string>() { ".mp4",".3gp" });
09. fs.DefaultFileExtension = ".mp4";
10. fs.SuggestedFileName = "Video" + DateTime.Today.ToString();
11. fs.SuggestedStartLocation = PickerLocationId.VideosLibrary;
12.
13. // Using storagefile object defined earlier in above method to save the file using filesavepicker.
14. fs.SuggestedSaveFile = sf;
15.
16. // Saving the file
17. var s = await fs.PickSaveFileAsync();
18. if (s != null)
19. {
20. using (var dataReader = new DataReader(rs.GetInputStreamAt(0)))
21. {
22. await dataReader.LoadAsync((uint)rs.Size);
23. byte[] buffer = new byte[(int)rs.Size];
24. dataReader.ReadBytes(buffer);
25. await FileIO.WriteBytesAsync(s, buffer);
26. }
27. }
28. }
29. catch (Exception ex)
30. {
31. var messageDialog = new MessageDialog("Something went wrong.");
32. await messageDialog.ShowAsync();
33. }
34. }

Now, run the program and enjoy!

Capturing Saving Video Windows 10 Application

Aditya
Bhardwaj
Blogger, Geek, FreeLancer, Developer

http://intellectape.com/

1015 56.5k

0 1

Type your comment here and press Enter Key (Minimum 18 characters)

Good One Sir


Saineshwar Bageri Sep 13,
2015
80 17.4k 12m 0 0 Reply

Philadelphia
New York
London
Delhi

J o i n C # C o r n e r
and millions of developer friends worldwide.

Enter your email address Sign Up

Learn ASP.NET MVC Learn ASP.NET Core Learn Python Learn JavaScript Learn Xamarin Learn Oracle More...
Home Events Consultants Jobs Career Advice Stories Partners
About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ
©2018 C# Corner. All contents are copyright of their authors.

Das könnte Ihnen auch gefallen