.net - 如何通过网络客户端下载图像 (jpg) 并保存到 Windows Phone 7 上的独立存储?

标签 .net silverlight windows-phone-7 webclient isolatedstorage

由于我缺乏编程经验(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

希望这会在 future 帮助其他沮丧的新手!
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();
}

最佳答案

试试这个,可能对你有帮助,

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);
                }
            }
        }
    });
}

关于.net - 如何通过网络客户端下载图像 (jpg) 并保存到 Windows Phone 7 上的独立存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7712160/

相关文章:

c# - 有什么办法可以规避 "Dialogs must be user-initiated"异常?

c# - 如何在 Sharepoint 2007 Web 部件中托管 Silverlight 应用程序

c# - Silverlight 5 动态流 URI 设置

visual-studio-2010 - 错误 'Newtonsoft.Json.Linq.JToken' 无法使用 TombstoneHelper 序列化

c# - 性能相交和不同的元素提取?

javascript - 通过严格的客户端用户体验驱动.NET/服务器端应用程序。

c# - 控件的事件到外部静态类方法

c# - Rhino mock 命名期望

c# - 在物理设备上测试

windows-phone-7 - 在 WP7 中使用 Caliburn.Micro 将 View 模型共享到多个 View