如何在Windows Phone 7上通过webclient下载图片(jpg)并保存到隔离存储?

5
由于我缺乏编程经验(3个月),我无法重新创建任何发现的上述问题的示例。 我找到的示例与非WP7 Silverlight、基于相机的图像保存相关,对我的需求过于复杂或根本无法工作。 我已经能够使用Webclient实例下载文本文件并使用StreamWriter将其保存到隔离存储中。 我需要用jpg图像完成同样的任务。 以下是我用来下载文本文件的代码。
 IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication();


    private void GetTextFile()
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new     DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://mywebsite.com/textfile.txt"));
        }

    private void client_DownloadStringCompleted(object sender,     DownloadStringCompletedEventArgs e)
        {
            StreamWriter MyStreamWriter = new StreamWriter(new     IsolatedStorageFileStream("textfile.txt", FileMode.Create, MyStore));
            MyStreamWriter.WriteLine(e.result)
            MyStreamWriter.Close();
    }

===============================================================================

我已删除一些处理错误等内容,以使它尽可能简单。

请有人修改上述内容,以使我能够下载并保存jpg文件?

请尽可能保持简单,因为我很容易混淆。

提前感谢您的时间!

已解决!===============================================================================

我成功地使用下面网站的信息使其工作。 http://dotnet.dzone.com/articles/operating-image-files-windows

希望这将帮助未来其他沮丧的新手!

IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication();

private void GetImageFile()
{
        WebClient client = new WebClient();
        client.OpenReadCompleted += new     OpenReadCompletedEventHandler(client_OpenReadCompleted);
        client.OpenReadAsync(new Uri("http://mywebsite.com/1.jpg"), client);
    }


void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
     var resInfo = new StreamResourceInfo(e.Result, null);
 var reader = new StreamReader(resInfo.Stream);
 byte[] contents;
     using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
    {
     contents = bReader.ReadBytes((int)reader.BaseStream.Length);
    }
     IsolatedStorageFileStream stream = MyStore.CreateFile("10.jpg");
 stream.Write(contents, 0, contents.Length);
 stream.Close();
}
1个回答

4

试试这个,也许对你有帮助。

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    HttpWebRequest reqest1 = (HttpWebRequest)WebRequest.Create(url);
    reqest1.BeginGetResponse(DownloadImageCallback, reqest1);
    Thread.Sleep(1000);
}

void DownloadImageCallback(IAsyncResult result)
{

    HttpWebRequest req1 = (HttpWebRequest)result.AsyncState;
    HttpWebResponse responce = (HttpWebResponse)req1.EndGetResponse(result);
    Stream s = responce.GetResponseStream();
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        string directory = "Images";
        if (!myStore.DirectoryExists(directory))
        {
            myStore.CreateDirectory(directory);
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//yourfilename.jpg"))
                {
                    var wb = new WriteableBitmap(bitimage);
                    var width = wb.PixelWidth;
                    var height = wb.PixelHeight;
                    Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                }
            }
        }
        else
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(directory + "//yourfilename.jpg"))
                {
                    myIsolatedStorage.DeleteFile(directory + "//yourfilename.jpg");
                }

                using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//yourfilename.jpg"))
                {
                    var wb = new WriteableBitmap(bitimage);
                    var width = wb.PixelWidth;
                    var height = wb.PixelHeight;
                    Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                }
            }
        }
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接