0
votes

I am trying to import a video into a UWP canvas. I have this code that successfully imports a picture: private async void AddImageButton_Click(object sender, RoutedEventArgs e) { Image MyImage = new Image();

        var picker = new FileOpenPicker();
        picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        picker.FileTypeFilter.Add(".jpg");
        picker.FileTypeFilter.Add(".png");

        StorageFile file = await picker.PickSingleFileAsync();

        if (file != null)
        {
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", file);
          //  var files = await file.GetFilesAsync();
            var bitmap = new BitmapImage();

            var stream = await file.OpenReadAsync();

            // AddHandler(, new ExceptionRoutedEventHandler(Bitmap_ImageFailed), true);
            bitmap.ImageFailed += Bitmap_ImageFailed;
            await bitmap.SetSourceAsync(stream);
            MyImage.Source = bitmap;

            AddHandler(ManipulationStartedEvent, new ManipulationStartedEventHandler(Image_ManipulationStarted), true);
            AddHandler(ManipulationDeltaEvent, new ManipulationDeltaEventHandler(Image_ManipulationDelta), true);
            AddHandler(ManipulationCompletedEvent, new ManipulationCompletedEventHandler(Image_ManipulationCompleted), true);

            ManipulationMode = ManipulationModes.All;
            MyImage.RenderTransform = ImageTransforms;


            parentCanvas.Children.Add(MyImage);
        }

    }

I tried adapting this to import a video but got stuck when converting the bitmap to a MediaPlayerElement. Any suggestions?

Thanks!

1

1 Answers

0
votes

According your above code of adding image to the Canvas, you can try the following code to add the MediaPlayerElement to the Canvas and use the FileOpenPicker to picker a media file as the MediaPlayerElement's source. You can make some modification to meet your requirement.

private async void AddMediaPlayerElementButton_Click_1(object sender, RoutedEventArgs e)
{
    var picker = new FileOpenPicker();
    picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    picker.FileTypeFilter.Add(".mp4");
    StorageFile file = await picker.PickSingleFileAsync();
    MediaPlayerElement mediaPlayer = new MediaPlayerElement() { AreTransportControlsEnabled = true };
    if (file != null)
    {
        mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
    }
    parentCanvas.Children.Add(mediaPlayer);
}