以下代碼讀取圖像,並將該圖像居中(剪切並複製)到帶有黑色框的結果圖像中,以返回新圖像。UWP - 如何在後臺任務中使用WriteableBimap?另類?
我可以不使用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;
}
一切工作正常,但後來我試圖把該代碼放到一個BackgroundTask在UWP,是由於這樣的事實結果的異常,我不能在不同的線程,則UI線程中使用WriteableBitmap的。
還有什麼我可以做的嗎?我需要在背景任務中運行它。
除了行:
result.Blit(destRect, bitmapToFrame, sourceRect);
實際使用WriteableBitmapEx擴展,我將無法使用。任何其他方式?我真的沒有看到如何操作後臺任務線程上的位圖。我很安靜地迷失在這裏。我一直在考慮使用openCV庫,只用C++來做,會解決問題嗎?
還有this SO Q&A,其中提到我應該從XamlRenderingBackgroundTask class推導出我的背景任務。我怎樣才能做到這一點?
如果我的任務是密封類,我該如何繼承?我沒有明白。 – pijemcolu
你不從*你的*任務繼承。你的任務繼承自XamlBackgroundRenderingTask – AlexDrenea