UWP 代码中有关 SoftwareBitmapSource 的异常
Exception about SoftwareBitmapSource in UWP Code
这段代码有什么问题?
private async void FrameMonitor_PartEvent(object sender, MonitorParam monitorParam)
{
SoftwareBitmap bmp = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)monitorParam.WP_FrameInfo.ImageWidth, (int)monitorParam.WP_FrameInfo.ImageHeight, BitmapAlphaMode.Ignore);
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(monitorParam.WP_FrameBytes);
bmp.CopyFromBuffer(buffer);
SoftwareBitmapSource bmpsource = new SoftwareBitmapSource();
await bmpsource.SetBitmapAsync(bmp);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, async () =>
{
Img_WPImg.Source = bmpsource;
});
bmp.Dispose();
}
关于这段代码:
SoftwareBitmapSource bmpsource = new SoftwareBitmapSource();
Visual Studio 显示此异常:
System.Exception: 'The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'
希望有高手帮帮我
该异常告诉我您需要从 UI 线程执行此操作,因此只需将该代码移到 Dispatcher 中,如下所示:
private async void FrameMonitor_PartEvent(object sender, MonitorParam monitorParam)
{
SoftwareBitmap bmp = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)monitorParam.WP_FrameInfo.ImageWidth, (int)monitorParam.WP_FrameInfo.ImageHeight, BitmapAlphaMode.Ignore);
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(monitorParam.WP_FrameBytes);
bmp.CopyFromBuffer(buffer);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, async () =>
{
SoftwareBitmapSource bmpsource = new SoftwareBitmapSource();
await bmpsource.SetBitmapAsync(bmp);
Img_WPImg.Source = bmpsource;
});
bmp.Dispose();
}
这段代码有什么问题?
private async void FrameMonitor_PartEvent(object sender, MonitorParam monitorParam)
{
SoftwareBitmap bmp = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)monitorParam.WP_FrameInfo.ImageWidth, (int)monitorParam.WP_FrameInfo.ImageHeight, BitmapAlphaMode.Ignore);
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(monitorParam.WP_FrameBytes);
bmp.CopyFromBuffer(buffer);
SoftwareBitmapSource bmpsource = new SoftwareBitmapSource();
await bmpsource.SetBitmapAsync(bmp);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, async () =>
{
Img_WPImg.Source = bmpsource;
});
bmp.Dispose();
}
关于这段代码:
SoftwareBitmapSource bmpsource = new SoftwareBitmapSource();
Visual Studio 显示此异常:
System.Exception: 'The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'
希望有高手帮帮我
该异常告诉我您需要从 UI 线程执行此操作,因此只需将该代码移到 Dispatcher 中,如下所示:
private async void FrameMonitor_PartEvent(object sender, MonitorParam monitorParam)
{
SoftwareBitmap bmp = new SoftwareBitmap(BitmapPixelFormat.Bgra8, (int)monitorParam.WP_FrameInfo.ImageWidth, (int)monitorParam.WP_FrameInfo.ImageHeight, BitmapAlphaMode.Ignore);
IBuffer buffer = WindowsRuntimeBufferExtensions.AsBuffer(monitorParam.WP_FrameBytes);
bmp.CopyFromBuffer(buffer);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, async () =>
{
SoftwareBitmapSource bmpsource = new SoftwareBitmapSource();
await bmpsource.SetBitmapAsync(bmp);
Img_WPImg.Source = bmpsource;
});
bmp.Dispose();
}