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

发布于 2024-12-08 21:56:12 字数 2408 浏览 0 评论 0原文

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

Due to my lack of programming experience (3 months) I have been unable to recreate any found examples of the above question. The examples I have found relate to non WP7 Silverlight, camera based saving of images, have been to complex for my needs or have just not worked. I have been able to download a text file using an instance of the Webclient and save it to isolated storage using StreamWriter. I need to acomplish the same task with jpg images. Below is what I have used to download the text file.

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

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

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

I have removed a few lines used to handle errors etc to keep it as simple as posible.

Please could someone modify the above to enable me to download and save a jpg?

Please keep it as simple as possible as I am easily confused.

Thank you for your time in advance!

RESOLVED!===============================================================================

I managed to get it working using information from this site below.
http://dotnet.dzone.com/articles/operating-image-files-windows

Hopefully this will help other frustrated newbie's in the 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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把回忆走一遍 2024-12-15 21:56:12

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

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

Try this one, Maybe helpfull to You,

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);
                }
            }
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文