如何通过 Web 客户端下载图像 (jpg) 并保存到 Windows Phone 7 上的独立存储?
由于我缺乏编程经验(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个,也许对你有帮助,
Try this one, Maybe helpfull to You,