首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何通过webclient下载图像(jpg)并保存到Windows Phone 7上的独立存储?

要在Windows Phone 7上通过WebClient下载图像并保存到独立存储,请按照以下步骤操作:

  1. 首先,确保已经在Windows Phone 7上安装了Visual Studio 2012。
  2. 在Visual Studio 2012中创建一个新的Windows Phone 7项目。
  3. 在项目中添加一个按钮,并为其设置一个名称,例如“downloadButton”。
  4. 在项目中添加一个文本框,并为其设置一个名称,例如“imageUrlTextBox”。
  5. 在项目中添加一个图像控件,并为其设置一个名称,例如“imageControl”。
  6. 在项目中添加一个文件夹,命名为“isolatedStorage”。
  7. 在“isolatedStorage”文件夹中创建一个新的文件,命名为“myImage.jpg”。
  8. 在项目中添加以下命名空间:
代码语言:csharp
复制
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
  1. 在“downloadButton”的单击事件中添加以下代码:
代码语言:csharp
复制
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));
}
  1. 在项目中添加以下代码:
代码语言:csharp
复制
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));
    }
}
  1. 运行项目,在文本框中输入图像的URL,然后单击“下载”按钮。
  2. 图像将被下载并保存到Windows Phone 7的独立存储中,然后显示在图像控件中。

请注意,这个答案中没有提及亚马逊AWS、Azure、阿里云、华为云、天翼云、GoDaddy、Namecheap、Google等流行的一些云计算品牌商。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券