要在Windows Phone 7上通过WebClient下载图像并保存到独立存储,请按照以下步骤操作:
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
private void downloadButton_Click(object sender, RoutedEventArgs e)
{
string imageUrl = imageUrlTextBox.Text;
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(new Uri(imageUrl));
}
private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
Stream imageStream = e.Result;
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
string imagePath = "isolatedStorage/myImage.jpg";
using (IsolatedStorageFileStream fileStream = storage.CreateFile(imagePath))
{
int byteSize = 4096;
byte[] bytes = new byte[byteSize];
int bytesRead = 0;
while ((bytesRead = imageStream.Read(bytes, 0, byteSize)) != 0)
{
fileStream.Write(bytes, 0, bytesRead);
}
}
imageControl.Source = new BitmapImage(new Uri(imagePath, UriKind.Relative));
}
}
请注意,这个答案中没有提及亚马逊AWS、Azure、阿里云、华为云、天翼云、GoDaddy、Namecheap、Google等流行的一些云计算品牌商。
领取专属 10元无门槛券
手把手带您无忧上云