Do thiếu kinh nghiệm lập trình (3 tháng), tôi không thể tạo lại bất kỳ ví dụ nào về câu hỏi trên. Các ví dụ tôi đã tìm thấy liên quan đến Silverlight không WP7, tiết kiệm hình ảnh dựa trên máy ảnh, đã được phức tạp cho nhu cầu của tôi hoặc có chỉ không làm việc. Tôi đã có thể tải về một tập tin văn bản bằng cách sử dụng một thể hiện của Webclient và lưu nó vào bộ nhớ bị cô lập bằng cách sử dụng StreamWriter. Tôi cần phải hoàn thành nhiệm vụ tương tự với hình ảnh jpg. Dưới đây là những gì tôi đã sử dụng để tải xuống tệp văn bản.Làm cách nào để tải xuống hình ảnh (jpg) qua webclient và lưu vào bộ nhớ bị cô lập trên Windows Phone 7?
============================================== =================================
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();
}
========== ================================================== ===================
Tôi đã xóa một vài dòng được sử dụng để xử lý các lỗi v.v. để giữ cho nó đơn giản như có thể thực hiện được.
Xin vui lòng ai đó có thể sửa đổi ở trên để cho phép tôi tải xuống và lưu jpg?
Hãy giữ nó đơn giản nhất có thể vì tôi dễ nhầm lẫn.
Cảm ơn bạn đã dành thời gian trước!
RESOLVED! ============================================ ===================================
Tôi quản lý để làm cho nó hoạt động bằng cách sử dụng thông tin từ trang web này phía dưới. http://dotnet.dzone.com/articles/operating-image-files-windows
Hy vọng điều này sẽ giúp những người mới thất vọng khác trong tương lai!
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();
}