我使用BitmapEncoder
类将捕获的摄像机视频帧转换为Stream。当使用DispatcherTimer在循环中执行此操作时,我的应用程序逐渐泄漏内存但从未恢复任何内存。
我使用this Microsoft示例作为基础。
泄漏的实际代码。
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
using (var videoFrame = new VideoFrame(BitmapPixelFormat.Rgba16, (int)previewProperties.Width, (int)previewProperties.Height))
{
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
{
// Collect the resulting frame
using (SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap)
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
//MEMORY LEAK
encoder.SetSoftwareBitmap(previewFrame);
await encoder.FlushAsync();
//DO something with the stream
}
}
}
}