C# 从UWP中的url获取WriteableBitmap?
因此,我的应用程序从一个url获取一个位图图像,这是可行的,然后我需要将该图像分配给一个可写位图。目前,我有以下几点:C# 从UWP中的url获取WriteableBitmap?,c#,win-universal-app,bitmapimage,writeablebitmap,storagefile,C#,Win Universal App,Bitmapimage,Writeablebitmap,Storagefile,因此,我的应用程序从一个url获取一个位图图像,这是可行的,然后我需要将该图像分配给一个可写位图。目前,我有以下几点: Debug.WriteLine("Poster opened for loading. Url=" + e); BitmapImage img = new BitmapImage(new Uri(e)); Backdrop.Source = img; img.ImageOpened += async (s, e1)
Debug.WriteLine("Poster opened for loading. Url=" + e);
BitmapImage img = new BitmapImage(new Uri(e));
Backdrop.Source = img;
img.ImageOpened += async (s, e1) =>
{
WriteableBitmap wbm = new WriteableBitmap(((BitmapImage)s).PixelWidth, ((BitmapImage)s).PixelHeight);
var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(e));
using (var stream = await storageFile.OpenReadAsync())
{
await wbm.SetSourceAsync(stream);
}
};
这是一段非常糟糕的代码,但在StorageFile.getfilefromsapplicationuriasync()
之前,这一切都是有效的,我被告知该值在一个意外的范围内。我猜这是因为它只接受“msappx://”格式,而不接受“http://”。有什么办法可以让它工作吗?最终以更干净的方式?
我以为这很容易,但这是一场噩梦。从昨天起我就一直在尝试这样做
另外,我知道这可能看起来像是重复的,但我在StackOverflow上完全找不到真正有效的东西。试试这个:
var httpClient = new HttpClient();
var buffer = await httpClient.GetBufferAsync(pictureUri);
var writeableBitmap = new WriteableBitmap(width, height);
using (var stream = buffer.AsStream())
{
await writeableBitmap.SetSourceAsync(stream.AsRandomAccessStream());
}
这个想法很管用,但我不得不改变它,使流像这样与uwp一起工作(httpClient没有.GetBufferAsync):var buffer=wait httpClient.GetByteArrayAsync(e);MemoryStream str=新的MemoryStream(缓冲区);UWP中有两个不同的HTTPClient,一个在Windows.Web.Http命名空间中,另一个在System.Net.Http中。据我记忆所及,Microsoft建议使用Windows.Web.Http中的方法,并且它确实有GetBufferAsync方法。但是你可以用任何一个作为我的例子。