[英]C# UWP WriteableBitmap to MediaClip conversion
I'm working with UWP app. 我正在使用UWP应用。 I'm generating set of WriteableBitmap's and after that, i want to add it to MediaComposition and play in video player.
我正在生成WriteableBitmap集,然后,我要将其添加到MediaComposition并在视频播放器中播放。 I found one way how to do this, but it's really slow.
我找到了一种方法来执行此操作,但速度确实很慢。 Here is my code:
这是我的代码:
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;
}
Function "WriteableBitmapToStorageFile" takes almost 400ms to proceed WriteableBitmap. 函数“ WriteableBitmapToStorageFile”花费将近400毫秒来进行WriteableBitmap。 Is there any option to do it faster?
有什么选择可以更快地做到吗? I know, that MediaClip can be created from IDirect3DSurface but i don't know how to convert to it from WriteableBitmap.
我知道,可以从IDirect3DSurface创建MediaClip,但我不知道如何从WriteableBitmap转换为它。
Ok as @Johnny Westlake mentioned i used CanvasBitmap to draw my images. 好吧,就像@Johnny Westlake提到的那样,我使用CanvasBitmap绘制图像。 But now, after drawing many images only last image is displayed.
但是现在,在绘制许多图像之后,仅显示最后一张图像。 Code:
码:
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;
}
(...) (......)
Later in the code: 稍后在代码中:
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;
After i did it many times at the end there was only one image in MediaOverlays 最后我做了很多次之后,MediaOverlays中只有一张图像
To create a video file by WriteableBitmap
and MediaClip
which created from IDirect3DSurface
, you can try the following code using CanvasBitmap
. 要使用通过
IDirect3DSurface
创建的WriteableBitmap
和MediaClip
创建视频文件,可以使用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.