C#UWP WriteableBitmap到MediaClip的转换

时间:2018-06-27 14:43:56

标签: c# uwp writeablebitmap

我正在使用UWP应用。我正在生成WriteableBitmap集,然后,我要将其添加到MediaComposition并在视频播放器中播放。我找到了一种方法来执行此操作,但速度确实很慢。这是我的代码:

var clip = await MediaClip.CreateFromImageFileAsync(await 
WriteableBitmapToStorageFile(wb), new TimeSpan(0, 0, 0, 0, 1));
(...)

private static async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap WB)
    {
        string FileName = "MyFile.";
        Guid BitmapEncoderGuid = BitmapEncoder.TiffEncoderId;
        FileName += "tiff";

        var file = await Windows.Storage.ApplicationData.Current.TemporaryFolder
            .CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);

        using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
            Stream pixelStream = WB.PixelBuffer.AsStream();
            byte[] pixels = new byte[pixelStream.Length];
            await pixelStream.ReadAsync(pixels, 0, pixels.Length);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                      (uint)WB.PixelWidth,
                      (uint)WB.PixelHeight,
                      96.0,
                      96.0,
                      pixels);
            await encoder.FlushAsync();
        }
        return file;
    }

函数“ WriteableBitmapToStorageFile”花费将近400毫秒来进行WriteableBitmap。有什么选择可以更快地做到吗?我知道可以从IDirect3DSurface创建MediaClip,但我不知道如何从WriteableBitmap转换为它。

好吧,就像@Johnny Westlake提到的那样,我使用CanvasBitmap绘制图像。但是现在,在绘制许多图像之后,仅显示最后一张图像。代码:

public static CanvasDevice device = new CanvasDevice();
public static CanvasRenderTarget renderer = new CanvasRenderTarget(device, 2048, 1080, 96);

public static CanvasBitmap cb = CanvasBitmap.CreateFromBytes(device, new byte[64*64*4], 64, 64, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalizedSrgb);

public static Rect dest = new Rect
    {
        Width = 2048,
        Height = 1080
    };

public static Rect src = new Rect
    {
        Width = 64,
        Height = 64
    };

private static CanvasBitmap GenerateHeatmap(byte[] renderedHeatmap )
{   
    cb.SetPixelBytes(renderedHeatmap);

    using (var ds = renderer.CreateDrawingSession())
        {
            ds.DrawImage(cb, dest, src, 1, CanvasImageInterpolation.Cubic, CanvasComposite.Copy);
    }
        return renderer;
    }

(...)

稍后在代码中:

cb = GenerateHeatmap(renderedHeatmap);
MediaOverlay mediaOverlay = new MediaOverlay(MediaClip.CreateFromSurface(cb, new TimeSpan(0, 0, 0, 0, 1)));
mediaOverlay.Position = overlayPosition;
mediaOverlay.Opacity = heatmapOpacity;

return mediaOverlay;

最后我做了很多次之后,MediaOverlays中只有一张图像

1 个答案:

答案 0 :(得分:0)

要使用由WriteableBitmap创建的MediaClipIDirect3DSurface创建视频文件,可以使用CanvasBitmap尝试以下代码。

private async void CreateVideoFromWritableBitmapAsync(List<WriteableBitmap> WBs)
{
    var mediaComposition = new MediaComposition();
    foreach (WriteableBitmap WB in WBs)
    {
        var pixels = WB.PixelBuffer.ToArray();
        CanvasBitmap bitmap = CanvasBitmap.CreateFromBytes(CanvasDevice.GetSharedDevice(), pixels,
                64, 64, DirectXPixelFormat.B8G8R8A8UIntNormalized);
        MediaClip mediaClip = MediaClip.CreateFromSurface(bitmap, TimeSpan.FromSeconds(1));
        mediaComposition.Clips.Add(mediaClip);
    }
    //Save the video
    var file =await ApplicationData.Current.LocalFolder.CreateFileAsync("WBVideo.mp4");
    await mediaComposition.SaveAsync(file);
    mediaComposition=await MediaComposition.LoadAsync(file);
    await mediaComposition.RenderToFileAsync(file);
}