【发布时间】:2016-06-10 11:12:57
【问题描述】:
以下代码读取图像,并将该图像居中(剪切和复制)到带有黑框的结果图像中,然后返回新图像。
我可以不使用 writeablebitmap 类吗?
private async Task<WriteableBitmap> FrameToCenter(StorageFile image, Size newSize)
{
WriteableBitmap result = new WriteableBitmap((int) newSize.Width,(int) newSize.Height);
WriteableBitmap bitmapToFrame = await StorageFileToSoftwareBitmap(image);
Rect sourceRect = new Rect(0, 0, bitmapToFrame.PixelWidth, bitmapToFrame.PixelHeight);
int a = (int) Math.Min(newSize.Height, newSize.Width);
Rect destRect = new Rect((int) ((_screenResolution.Width - _screenResolution.Height)/2), 0, a, a);
result.Blit(destRect, bitmapToFrame, sourceRect);
return result;
}
一切正常,但后来我尝试将该代码放入 UWP 中的 BackgroundTask 中,由于我不能在与 UI 线程不同的线程上使用 WriteableBitmap,因此导致异常。
还有什么我可以做的吗?不过我需要在后台任务中运行它。
还有一行:
result.Blit(destRect, bitmapToFrame, sourceRect);
实际上正在使用我无法使用的 WriteableBitmapEx 扩展。还有什么办法吗?我真的没有看到如何在后台任务线程上操作位图的方法。我安静地迷失在这里。我什至一直在考虑使用 openCV 库并在 C++ 中进行,这样可以解决问题吗?
还有this SO Q&A,它提到我应该从XamlRenderingBackgroundTask class 派生我的后台任务。我该怎么做?
【问题讨论】:
标签: c# opencv uwp background-task writeablebitmap