Mxtin Mxtin - 1 month ago 23
C# Question

UWP make a collection of bitmaps to a video

I picked some jpg images and add these images to the MediaComposition(composition).

It works. I got the count of clips.

private async void encodeImages_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
foreach (string extension in FileExtensions.Image)
{
openPicker.FileTypeFilter.Add(extension);
}
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();

composition = new MediaComposition();
foreach (StorageFile file in files)
{
m_files.Add(file);
var clip =await MediaClip.CreateFromImageFileAsync(file, TimeSpan.Zero);
composition.Clips.Add(clip);
}

Debug.WriteLine(composition.Clips.Count);
}


Then I want to use MediaComposition generate a MediaStreamSource.

private void showVideo_Click(object sender, RoutedEventArgs e)
{
GC.Collect();

//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();
//await composition.RenderToFileAsync(videoFile);

encodeImagesResult.Position = TimeSpan.Zero;
IMediaSource mediaStreamSource = composition.GeneratePreviewMediaStreamSource(400, 400);
encodeImagesResult.SetMediaStreamSource(mediaStreamSource);
}


It will show the error "The parameter is incorrect."

What's wrong with my code? Many thanks!

Answer

The error The parameter is incorrect. here means the parameter in MediaClip.CreateFromImageFileAsync method is incorrect. In this method, the second parameter originalDuration represent how long to display the image in the video clip. And for a video clip, its duration can't be zero.

So to fix this error, you can edit this line:

var clip =await MediaClip.CreateFromImageFileAsync(file, TimeSpan.Zero);

and set the TimeSpan to the value that you want the image to be displayed for like:

var clip = await MediaClip.CreateFromImageFileAsync(file, TimeSpan.FromSeconds(1)); 

After this, your code should be able to work.