gpt4 book ai didi

c# - 将 SoftwareBitmap 转换为 WriteableBitmap

转载 作者:可可西里 更新时间:2023-11-01 13:30:07 25 4
gpt4 key购买 nike

每当我想将我的 SoftwareBitmap 转换为 WriteableBitmap 时,我都会收到以下异常:System.Runtime.InteropServices.COMException

这是我的代码片段:

 private async void Start(object sender, RoutedEventArgs e)
{

_MediaCapture = new MediaCapture();
await _MediaCapture.InitializeAsync();

mediaElement.Source = _MediaCapture;
await _MediaCapture.StartPreviewAsync();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 1);
timer.Tick += HandleTimerTick;
timer.Start();
}

private async void HandleTimerTick(object Sender, object E)
{


var frame = await _MediaCapture.GetPreviewFrameAsync();
SoftwareBitmap frameBitmap = frame.SoftwareBitmap;
WriteableBitmap bitmap = new WriteableBitmap(frameBitmap.PixelWidth, frameBitmap.PixelHeight);
try
{
frameBitmap.CopyToBuffer(bitmap.PixelBuffer);
}
catch (Exception)
{
Debug.WriteLine("Exception ");
}
}

线

frameBitmap.CopyToBuffer(bitmap.PixelBuffer); 

正在抛出异常。

我正在 x64 RemoteDevice 上调试它。

最佳答案

我可以使用您的代码重现此问题。这是由框架引起的。SoftwareBitmap 始终返回 null。

您可以使用以下代码解决此问题:

    private async void button_Click(object sender, RoutedEventArgs e)
{
_mediaCapture = new MediaCapture();

await _mediaCapture.InitializeAsync();

mediaElement.Source = _mediaCapture;

await _mediaCapture.StartPreviewAsync();

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 1);
timer.Tick += Timer_Tick;
timer.Start();
}

private async void Timer_Tick(object sender, object e)
{
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

var frame = await _mediaCapture.GetPreviewFrameAsync(videoFrame);

SoftwareBitmap frameBitmap = frame.SoftwareBitmap;

WriteableBitmap bitmap = new WriteableBitmap(frameBitmap.PixelWidth, frameBitmap.PixelHeight);

frameBitmap.CopyToBuffer(bitmap.PixelBuffer);

Debug.WriteLine("done");
}

关于c# - 将 SoftwareBitmap 转换为 WriteableBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33386180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com