Win8 Metro calls the camera to record the video and save the video in the appropriate location

Source: Internet
Author: User
Tags blank page

The previous article introduced the WIN8 Metro call camera to take photos and save photos in the corresponding location of the function, then this article is mainly about recording video, in fact, this is similar, the idea is the same, because the picture and video can be converted to stream files, Then they are essentially the same, but there are some different places, so let's introduce the difference in this particular place.

Here's the code for the Metro UI:

<page x:class= "Camera1.mainpage" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "HT Tp://schemas.microsoft.com/winfx/2006/xaml "xmlns:local=" Using:camera1 "xmlns:d=" http://schemas.microsoft.com/ expression/blend/2008 "xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/2006 "mc:ignorable=" D "  > <grid background= "{ThemeResource Applicationpagebackgroundthemebrush}" > <button x:name= "Btncamera" content= "Open Camera" horizontalalignment= "left" margin= "67,179,0,0" verticalalignment= "Top" click= "Btncamera_click"/ > <mediaelement x:name= "Media" horizontalalignment= "left" height= "604" margin= "273,45,0,0" verticalalignment= "Top" width= "746"/> <button x:name= "btnsave" content= "Save Video" horizontalalignment= "left" margin= "67,316,0,0" Ve Rticalalignment= "Top" click= "Btnsave_click"/> <textbox x:name= "TxtBox1" horizontalalignment= "left" Margin= "70 , 440,0,0 "textwrapping=" Wrap "verticalalignment= "Top"/> </Grid></Page> 

Here is the control code for the UI:

Using system;using system.collections.generic;using system.io;using system.linq;using System.runtime.interopservices.windowsruntime;using windows.foundation;using Windows.Foundation.Collections; Using windows.ui.xaml;using windows.ui.xaml.controls;using windows.ui.xaml.controls.primitives;using Windows.ui.xaml.data;using windows.ui.xaml.input;using windows.ui.xaml.media;using Windows.UI.Xaml.Navigation; Using windows.media.capture;using windows.ui.xaml.media.imaging;using windows.storage;using Windows.storage.streams;using windows.storage.pickers;//the Blank Page item template is documented at/HTTP go.microsoft.com/fwlink/? Linkid=234238namespace camera1{//<summary>//An empty page so can be used on it own or navigated to WI    Thin a Frame.        </summary> public sealed partial class Mainpage:page {private StorageFile file = null; Public MainPage () {this.        InitializeComponent (); } Private Async void BtncamerA_click (object sender, RoutedEventArgs e) {cameracaptureui dialog = new Cameracaptureui (); Dialog.            Videosettings.format = Cameracaptureuivideoformat.mp4; File = Await dialog.            Capturefileasync (Cameracaptureuimode.video); if (file = null) {Irandomaccessstream FileStream = await file.                OpenAsync (Fileaccessmode.read); Media. SetSource (Filestream,file.            ContentType); }} private Async void Btnsave_click (object sender, RoutedEventArgs e) {F                Ilesavepicker picker = new Filesavepicker ();                Picker.commitbuttontext = "Save"; Picker.                Suggestedfilename = "Hello"; Picker.                Filetypechoices.add ("Picture", New string[]{". mp4", ". avi", ". mkv", ". mpg", ". rmvb"}); Picker.                Suggestedstartlocation = pickerlocationid.videoslibrary; StorageFile FilePath = await picker.                Picksavefileasync (); if (fiLepath! = null) {var streamrandom = await file.                    OpenAsync (Fileaccessmode.read);                    IBuffer buffer = Randomaccessstreamtobuffer (streamrandom);                Await Fileio.writebufferasync (filePath, buffer); }}//Writes the video to the buffer private IBuffer randomaccessstreamtobuffer (Irandomaccessstream randomstre AM) {Stream stream = Windowsruntimestreamextensions.asstreamforread (randomstream.            Getinputstreamat (0));            MemoryStream MemoryStream = new MemoryStream ();                if (stream! = null) {byte[] bytes = Convertstreamtobyte (stream);                    if (bytes! = null) {var binaryWriter = new BinaryWriter (MemoryStream);                BinaryWriter.Write (bytes); }} IBuffer buffer = Windowsruntimebufferextensions.getwindowsruntimebuffer (MemoryStream, 0, (int) Me Morystream.lengTH);        return buffer; }//Change the flow to binary public static byte[] Convertstreamtobyte (Stream input) {byte[] buffer = new            byte[1024 * 1024];                using (MemoryStream ms = new MemoryStream ()) {int read; while (read = input. Read (buffer, 0, buffer. Length)) > 0) {Ms.                Write (buffer, 0, read); } return Ms.            ToArray (); }        }    }}

The program can be fully operational, I hope everyone is not hesitate to enlighten.

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.