如何有效地从RenderTargetBitmap创建MediaClip? [英] How to create a MediaClip from RenderTargetBitmap in an efficent way?

查看:76
本文介绍了如何有效地从RenderTargetBitmap创建MediaClip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c#在UWP应用中捕获XAML网格的视频.

I am trying to capture a video of a XAML grid in UWP app using c#.

我的方法.

1.使用RenderTargetBitmap使用renderTargetBitmap.RenderAsync

1.Use RenderTargetBitmap to take a screenshot using renderTargetBitmap.RenderAsync

2.将数据转换为字节数组.

2.Convert the data to byte array.

3.使用字节创建一个图像文件,然后使用BitmapEncoder

3.Create an image file with the bytes and save it on disk using BitmapEncoder

4.使用MediaClip.CreateFromImageFileAsync

5.将剪辑添加到MediaComposition composition.Clips.Add(clip)

5.Add the clips to a MediaComposition composition.Clips.Add(clip)

6.使用composition.RenderToFileAsync(video);

现在这种方法行之有效.

Now this approach works.

但是,您可以想像到磁盘上保存图像,然后读取它们以创建剪辑,它的SLOWWWW和帧速率很低.

But as you can imagine, going to disk to save the images and then read them to create the Clips, its SLOWWWW and the framerate is low.

我正在寻找可以避免每个屏幕截图都进入磁盘的内容. 可以将RenderTargetBitmap(或IBufferbyte[])转换为MediaClip而不用到磁盘的东西,或采用其他方法来保存视频.

I am looking for something that avoids going to the disk per each screenshot. Something that converts a RenderTargetBitmap (or IBuffer or byte[]) to MediaClips, without going to the disk, or some different approach to save the video.

我正在为 Hololens 开发 UWP应用.

推荐答案

尝试如下操作:

与您所做的相同.

using (var soft = SoftwareBitmap.CreateCopyFromBuffer(pixels, BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight, BitmapAlphaMode.Premultiplied))
{
   CanvasBitmap canvas = CanvasBitmap.CreateFromSoftwareBitmap(CanvasDevice.GetSharedDevice(), soft); 

   MediaClip m = MediaClip.CreateFromSurface(canvas, DateTime.Now - previousFrame); 
   composition.Clips.Add(m); 
}

请记住以捕捉设备丢失的异常并创建新设备

Remember to catch the device lost exceptions and create a new device

这篇关于如何有效地从RenderTargetBitmap创建MediaClip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆