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

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

问题描述

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

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

我的方法.

1.Use 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);

现在这种方法有效.

但正如您想象的那样,去磁盘保存图像,然后读取它们以创建剪辑,它的 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天全站免登陆