C# 无法在windows phone 8中使用SaveJpeg方法(NotSupportedException)
我使用下面的代码在WindowsPhone8中保存远程图像。但我一直在使用C# 无法在windows phone 8中使用SaveJpeg方法(NotSupportedException),c#,image,windows-phone-8,C#,Image,Windows Phone 8,我使用下面的代码在WindowsPhone8中保存远程图像。但我一直在使用System.NotSupportedException:不支持指定的方法。SaveJpeg()方法调用时出现异常 我尝试了不同的方法调用组合(您可以看到注释行)。我无法找出我做错了什么 using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(imageUrl);
System.NotSupportedException:不支持指定的方法。
SaveJpeg()方法调用时出现异常
我尝试了不同的方法调用组合(您可以看到注释行)。我无法找出我做错了什么
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(imageUrl);
await Task.Run(async () =>
{
if (response.IsSuccessStatusCode)
{
// save image locally
Debug.WriteLine("Downloading image..." + imageName);
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!myIsolatedStorage.DirectoryExists("Images"))
myIsolatedStorage.CreateDirectory("Images");
string path = imageName;
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(path);
var buffer = await response.Content.ReadAsStreamAsync();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
BitmapImage bitmap = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
bitmap.SetSource(buffer);
WriteableBitmap wb = new WriteableBitmap(bitmap);
//System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 98);
});
fileStream.Close();
}
}
});
}
通过将代码块放在BeginInvoke块中,可以在调用
fileStream.Close()
的代码的不同线程(“UI线程”)上调用SaveJpeg
实际上,这意味着对fileStream.Close()
的调用很可能在wb.SaveJpeg
之前调用
如果将fileStream.Close()
移动到BeginInvoke块中,在wb.SaveJpeg()之后,它应该可以工作