private async void Button_Click(object sender, RoutedEventArgs e) { var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".png"); picker.FileTypeFilter.Add(".bmp"); picker.FileTypeFilter.Add(".gif"); var imageFiles = await picker.PickMultipleFilesAsync(); if (imageFiles.Count == 0) { TextLog.Text += "\nNo file selected"; return; } TextLog.Text += "\nCreating video file"; var file = await KnownFolders.VideosLibrary.CreateFileAsync("PictureMovie.mp4", CreationCollisionOption.GenerateUniqueName); using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { TextLog.Text += String.Format("\nCreated {0}", file.Path); using (var writer = new PictureWriter(outputStream, _width, _height, TimeSpan.FromSeconds(1))) { foreach (var imageFile in imageFiles) { TextLog.Text += String.Format("\nAdding {0}", imageFile.Path); using (var inputStream = await imageFile.OpenAsync(FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(inputStream); var transform = new BitmapTransform(); transform.ScaledHeight = _height; transform.ScaledWidth = _width; var pixels = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb ); writer.AddFrame(pixels.DetachPixelData()); } } } } TextLog.Text += "\nDone"; }
/// <summary> /// Handle the returned file from file picker /// This method is triggered by ContinuationManager based on ActivationKind /// </summary> /// <param name="args">File save picker continuation activation argment. It cantains the file user selected with file save picker </param> public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args) { StorageFile videoFile = args.File; if (videoFile != null) { IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite); m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight); // Add frames to the video. ProcessVideoRing.IsActive = true; VideoElement.AreTransportControlsEnabled = false; ImageBtn.IsEnabled = false; EncodeBtn.IsEnabled = false; statusText.Text = "Encoding..."; foreach (StorageFile file in m_files) { Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync(); float scaleOfWidth = (float)m_videoWidth / properties.Width; float scaleOfHeight = (float)m_videoHeight / properties.Height; float scale = scaleOfHeight > scaleOfWidth ? scaleOfWidth : scaleOfHeight; uint width = (uint)(properties.Width * scale); uint height = (uint)(properties.Height * scale); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { for (int i = 0; i < 10; ++i) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); PixelDataProvider dataProvider = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform { ScaledWidth = width, ScaledHeight = height }, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height); } } } m_picture.Finalize(); m_picture = null; VideoElement.AreTransportControlsEnabled = true; ImageBtn.IsEnabled = true; EncodeBtn.IsEnabled = true; statusText.Text = "The image files are encoded successfully. You can review the video."; ProcessVideoRing.IsActive = false; videoStream.Dispose(); videoStream = null; videoStream = await videoFile.OpenAsync(FileAccessMode.Read); VideoElement.SetSource(videoStream, videoFile.ContentType); } }
private async void CreateAndSaveVideo() { if (m_files != null && m_files.Count > 0) { if (_isDateNeeded) { SortPhoto(); } StorageFolder myVideoLibrary; //если папка существует открываем ,если нет создаем try { myVideoLibrary = await KnownFolders.PicturesLibrary.GetFolderAsync("ImageConvertor"); } catch { myVideoLibrary = await KnownFolders.PicturesLibrary.CreateFolderAsync("ImageConvertor"); } StorageFile videoFile = await myVideoLibrary.CreateFileAsync(_videoName + ".mp4", CreationCollisionOption.GenerateUniqueName); if (videoFile != null) { PickPhotoBtn.IsEnabled = false; Encode.IsEnabled = false; dateCheckBox.IsChecked = false; durationList.IsEnabled = false; nameBox.IsEnabled = false; BackBar.IsEnabled = false; progressRing.IsActive = true; titleBlock.Text = "Конвертирование . . ."; try { using (IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite)) { m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight); foreach (StorageFile file in m_files) { Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync(); var longitude = properties.Longitude; float scaleOfWidth = (float)m_videoWidth / properties.Width; float scaleOfHeight = (float)m_videoHeight / properties.Height; float scale = scaleOfHeight > scaleOfWidth ? scaleOfWidth : scaleOfHeight; uint width = (uint)(properties.Width * scale); uint height = (uint)(properties.Height * scale); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { for (int i = 0; i < 10 * _durationSec; ++i) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); PixelDataProvider dataProvider = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform { ScaledWidth = width, ScaledHeight = height }, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height); } } } m_picture.Finalize(); m_picture = null; } } catch (Exception ex) { CreateAndSaveVideo(); } progressRing.IsActive = false; await CreateVideoModelAndWrite(videoFile); //сохраняем данные о файле в локальную папку Frame.Navigate(typeof(MainPage)); } } }
private async void EncodeBtn_Click(object sender, RoutedEventArgs e) { if (m_files.Count == 0) { statusText.Text = "You must select one image at least."; return; } // Create the video file via file picker. FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; savePicker.FileTypeChoices.Add("MP4 File", new List <string>() { ".mp4" }); savePicker.SuggestedFileName = "output"; StorageFile videoFile = await savePicker.PickSaveFileAsync(); if (videoFile != null) { IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite); m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight); // Add frames to the video. ProcessVideoRing.IsActive = true; statusText.Text = "Encoding..."; foreach (StorageFile file in m_files) { Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync(); float scaleOfWidth = (float)m_videoWidth / properties.Width; float scaleOfHeight = (float)m_videoHeight / properties.Height; float scale = scaleOfHeight > scaleOfWidth ? scaleOfWidth : scaleOfHeight; uint width = (uint)(properties.Width * scale); uint height = (uint)(properties.Height * scale); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { for (int i = 0; i < 10; ++i) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); PixelDataProvider dataProvider = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform { ScaledWidth = width, ScaledHeight = height }, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height); } } } m_picture.Finalize(); m_picture = null; statusText.Text = "The image files are encoded successfully. You can review the video."; ProcessVideoRing.IsActive = false; videoStream.Dispose(); videoStream = null; videoStream = await videoFile.OpenAsync(FileAccessMode.Read); VideoElement.SetSource(videoStream, videoFile.ContentType); } }