예제 #1
0
        private async void OnConvert(object sender, RoutedEventArgs e)
        {
            var gifCreator = new GifCreator();

            var picker = new FileSavePicker
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                DefaultFileExtension   = ".gif",
                SuggestedFileName      = Path.ChangeExtension(sourceFile.Name, ".gif")
            };

            var resources = ResourceLoader.GetForCurrentView();

            picker.FileTypeChoices.Add(resources.GetString("GIFImages"), new[] { ".gif" }.ToList());

            var destinationFile = await picker.PickSaveFileAsync();

            if (destinationFile != null)
            {
                ProgressBar.Value = 0.0;

                ProgressRing.IsActive  = true;
                ProgressBar.Visibility = Visibility.Visible;

                CancelButton.Visibility = Visibility.Visible;

                var videoProperties = await sourceFile.Properties.GetVideoPropertiesAsync();

                try
                {
                    _action = gifCreator.TranscodeGifAsync(sourceFile, destinationFile, videoProperties.Width,
                                                           videoProperties.Height);

                    _action.Progress = OnProgress;

                    await _action;

                    if (_action.Status == AsyncStatus.Completed)
                    {
                        await Launcher.LaunchFileAsync(destinationFile);
                    }
                }
                catch (TaskCanceledException)
                {
                }
                finally
                {
                    _action = null;

                    ProgressRing.IsActive   = false;
                    CancelButton.Visibility = Visibility.Collapsed;
                    ProgressBar.Visibility  = Visibility.Collapsed;
                }
            }
        }
예제 #2
0
        private async Task TranscodeSingleFileAsync()
        {
            var firstVideo = _sourceFiles.First();

            var sourceFile = firstVideo.File;

            var gifCreator = new GifCreator();

            var picker = new FileSavePicker
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                DefaultFileExtension   = ".gif",
                SuggestedFileName      = Path.ChangeExtension(sourceFile.Name, ".gif")
            };

            var resources = ResourceLoader.GetForCurrentView();

            picker.FileTypeChoices.Add(resources.GetString("GIFImages"), new[] { ".gif" }.ToList());

            var destinationFile = await picker.PickSaveFileAsync();

            if (destinationFile != null)
            {
                Initialize();

                var videoProperties = await sourceFile.Properties.GetVideoPropertiesAsync();

                try
                {
                    _operation = gifCreator.TranscodeGifAsync(sourceFile, destinationFile, firstVideo.Width,
                                                              firstVideo.Height, firstVideo.FrameRate);

                    _operation.Progress = OnProgress;

                    var succeeded = await _operation;

                    if (succeeded && _operation.Status == AsyncStatus.Completed)
                    {
                        await Launcher.LaunchFileAsync(destinationFile);
                    }
                }
                catch (TaskCanceledException)
                {
                }
                finally
                {
                    Cleanup();
                }
            }
        }
예제 #3
0
        private async Task TranscodeMultipleFilesAsync()
        {
            var picker = new FolderPicker
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                ViewMode = PickerViewMode.List,
            };

            picker.FileTypeFilter.Add(".gif");

            var folder = await picker.PickSingleFolderAsync();

            if (folder == null)
            {
                return;
            }

            Initialize();

            try

            {
                _operation = AsyncInfo.Run(async delegate(CancellationToken token, IProgress <double> progress)
                {
                    var progressPerFile = 100 / Convert.ToDouble(_sourceFiles.Count);

                    progress.Report(0.0);

                    var fileIndex = 0.0;

                    foreach (var item in _sourceFiles)
                    {
                        var sourceFile = item.File;

                        var desiredName = Path.ChangeExtension(item.Name, ".gif");

                        var destinationFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.GenerateUniqueName);

                        if (token.IsCancellationRequested)
                        {
                            return(false);
                        }

                        try
                        {
                            var gifCreator = new GifCreator();

                            var action = gifCreator.TranscodeGifAsync(sourceFile, destinationFile, item.Width,
                                                                      item.Height, item.FrameRate);

                            action.Progress = delegate(IAsyncOperationWithProgress <bool, double> a, double v)
                            {
                                if (token.IsCancellationRequested)
                                {
                                    action.Cancel();
                                }

                                progress.Report((fileIndex * progressPerFile) + progressPerFile * v / 100.0);
                            };

                            await action;

                            if (token.IsCancellationRequested)
                            {
                                return(false);
                            }
                        }
                        catch (System.Exception se)
                        {
                            System.Diagnostics.Debug.WriteLine(se.Message);
                        }

                        fileIndex++;
                    }

                    return(true);
                });

                _operation.Progress = OnProgress;

                await _operation;

                await Launcher.LaunchFolderAsync(folder);
            }
            catch (TaskCanceledException)
            {
            }
            finally
            {
                Cleanup();
            }
        }