How do I download images (jpg) via a webclient and save to isolated storage on Windows Phone 7?
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 (Bi开发者_如何学PythonnaryReader 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();
}
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);
}
}
}
});
}
精彩评论