I'm trying to create a MediaComposition. I have succeeded in combining multiple png images into a single video; however, the files that's created has a black background. At first I thought this might be because the files were png files, but the same bevaviour occurs for jpgs. The following is how I'm saving the image:
public async Task<bool> Save(InkCanvas canvas, StorageFile file)
{
if (canvas != null && canvas.InkPresenter.StrokeContainer.GetStrokes().Count > 0)
{
if (file != null)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await canvas.InkPresenter.StrokeContainer.SaveAsync(stream);
}
}
Clear(canvas);
return true;
}
return false;
}
It saves the image fine, but the background is alpha. What this means is that when I try and chain these together into a media composition, there is no background, and it renders as black. I have tried using overlays when creating the MediaComposition to correct this:
MediaClip overlayVideoClip = MediaClip.CreateFromColor(Colors.White, new TimeSpan(0, 1, 0));
MediaOverlay mo = new MediaOverlay(overlayVideoClip);
MediaOverlayLayer mol = new MediaOverlayLayer();
mol.Overlays.Add(mo);
composition.OverlayLayers.Add(mol);
But to no avail. My suspicion is that I'm misunderstanding the meaning of the term overlay in this case. So, my questions are: is it possible to overlay the video at composition time and, if so, how? Alternatively, if this needs to be done in the image itself, how can I save the image with a background?
EDIT:
I've made progress (?) with this; the following compiles and runs, but creates a solid black image:
public async Task TestSave(InkCanvas canvas, StorageFile file)
{
RenderTargetBitmap rtb =
new RenderTargetBitmap();
PixelFormats.Pbgra32);
await rtb.RenderAsync(canvas);
var pixelBuffer = await rtb.GetPixelsAsync();
using (IRandomAccessStream stream =
await file.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder encoder =
await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)rtb.PixelWidth,
(uint)rtb.PixelHeight,
96d, 96d,
pixelBuffer.ToArray());
await encoder.FlushAsync();
}
}
EDIT:
I found this answer, which sort of solves the problem by using the Win2D library; although it doesn't address my actual issue, it lets me let around it. Hopefully there is a better solution out there.