diff --git a/Samples/MediaPlayerCPP/MainPage.xaml.cpp b/Samples/MediaPlayerCPP/MainPage.xaml.cpp index dbae09153c..f9d97678c5 100644 --- a/Samples/MediaPlayerCPP/MainPage.xaml.cpp +++ b/Samples/MediaPlayerCPP/MainPage.xaml.cpp @@ -240,15 +240,27 @@ task MainPage::ExtractFrame() filePicker->SuggestedStartLocation = PickerLocationId::VideosLibrary; filePicker->DefaultFileExtension = ".jpg"; filePicker->FileTypeChoices->Insert("Jpeg file", ref new Platform::Collections::Vector(1, ".jpg")); + filePicker->FileTypeChoices->Insert("Png file", ref new Platform::Collections::Vector(1, ".png")); + filePicker->FileTypeChoices->Insert("Bmp file", ref new Platform::Collections::Vector(1, ".bmp")); // Show file picker so user can select a file auto file = co_await filePicker->PickSaveFileAsync(); if (file != nullptr) { - auto stream = co_await file->OpenAsync(FileAccessMode::ReadWrite); - // encode frame as jpeg file - co_await frame->EncodeAsJpegAsync(stream); - stream = nullptr; + auto outputStream = co_await file->OpenAsync(FileAccessMode::ReadWrite); + if (file->FileType == ".jpg") + { + co_await frame->EncodeAsJpegAsync(outputStream); + } + else if (file->FileType == ".png") + { + co_await frame->EncodeAsPngAsync(outputStream); + } + else + { + co_await frame->EncodeAsBmpAsync(outputStream); + } + outputStream = nullptr; // launch file after creation auto launched = co_await Windows::System::Launcher::LaunchFileAsync(file); diff --git a/Samples/MediaPlayerCS/MainPage.xaml.cs b/Samples/MediaPlayerCS/MainPage.xaml.cs index 079cbc57eb..62fda22f0b 100644 --- a/Samples/MediaPlayerCS/MainPage.xaml.cs +++ b/Samples/MediaPlayerCS/MainPage.xaml.cs @@ -281,6 +281,7 @@ private async void ExtractFrame(object sender, RoutedEventArgs e) try { + using (frameGrabber) using (var frame = await frameGrabber.ExtractVideoFrameAsync(mediaPlayer.PlaybackSession.Position, exactSeek)) { var filePicker = new FileSavePicker(); @@ -288,13 +289,28 @@ private async void ExtractFrame(object sender, RoutedEventArgs e) filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; filePicker.DefaultFileExtension = ".jpg"; filePicker.FileTypeChoices["Jpeg file"] = new[] { ".jpg" }.ToList(); + filePicker.FileTypeChoices["Png file"] = new[] { ".png" }.ToList(); + filePicker.FileTypeChoices["Bmp file"] = new[] { ".bmp" }.ToList(); var file = await filePicker.PickSaveFileAsync(); if (file != null) { - var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite); - await frame.EncodeAsJpegAsync(outputStream); - outputStream.Dispose(); + using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite)) + { + if (file.FileType == ".jpg") + { + await frame.EncodeAsJpegAsync(outputStream); + } + else if (file.FileType == ".png") + { + await frame.EncodeAsPngAsync(outputStream); + } + else + { + await frame.EncodeAsBmpAsync(outputStream); + } + } + bool launched = await Windows.System.Launcher.LaunchFileAsync(file, new LauncherOptions() { DisplayApplicationPicker = false }); if (!launched) { @@ -302,7 +318,6 @@ private async void ExtractFrame(object sender, RoutedEventArgs e) } } } - frameGrabber?.Dispose(); } catch (Exception ex) { diff --git a/Samples/MediaPlayerWinUI/MainPage.xaml.cs b/Samples/MediaPlayerWinUI/MainPage.xaml.cs index a57dbeefee..e14eb332ff 100644 --- a/Samples/MediaPlayerWinUI/MainPage.xaml.cs +++ b/Samples/MediaPlayerWinUI/MainPage.xaml.cs @@ -274,6 +274,7 @@ private async void ExtractFrame(object sender, RoutedEventArgs e) try { + using (frameGrabber) using (var frame = await frameGrabber.ExtractVideoFrameAsync(mediaPlayer.PlaybackSession.Position, exactSeek)) { var filePicker = new FileSavePicker(); @@ -283,13 +284,28 @@ private async void ExtractFrame(object sender, RoutedEventArgs e) filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; filePicker.DefaultFileExtension = ".jpg"; filePicker.FileTypeChoices["Jpeg file"] = new[] { ".jpg" }.ToList(); + filePicker.FileTypeChoices["Png file"] = new[] { ".png" }.ToList(); + filePicker.FileTypeChoices["Bmp file"] = new[] { ".bmp" }.ToList(); var file = await filePicker.PickSaveFileAsync(); if (file != null) { - var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite); - await frame.EncodeAsJpegAsync(outputStream); - outputStream.Dispose(); + using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite)) + { + if (file.FileType == ".jpg") + { + await frame.EncodeAsJpegAsync(outputStream); + } + else if (file.FileType == ".png") + { + await frame.EncodeAsPngAsync(outputStream); + } + else + { + await frame.EncodeAsBmpAsync(outputStream); + } + } + bool launched = await Windows.System.Launcher.LaunchFileAsync(file, new LauncherOptions() { DisplayApplicationPicker = false }); if (!launched) { @@ -297,7 +313,6 @@ private async void ExtractFrame(object sender, RoutedEventArgs e) } } } - frameGrabber?.Dispose(); } catch (Exception ex) { diff --git a/Source/VideoFrame.h b/Source/VideoFrame.h index 037a23048f..d317f13b81 100644 --- a/Source/VideoFrame.h +++ b/Source/VideoFrame.h @@ -54,7 +54,7 @@ namespace winrt::FFmpegInteropX::implementation encoderValue.SetPixelData( BitmapPixelFormat::Bgra8, - BitmapAlphaMode::Ignore, + BitmapAlphaMode::Straight, pixelWidth, pixelHeight, 72,