Question

I want to download an image from an url and then save it, as image file, in the isolate storage. I already save there some string values, but i don't know how to save an image file. Thanks!

Was it helpful?

Solution

You can do it also through binary writer as ;

byte[] imageBytes;
HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
WebResponse imageResponse = imageRequest.GetResponse();

Stream responseStream = imageResponse.GetResponseStream();

using (BinaryReader br = new BinaryReader(responseStream ))
{
    imageBytes = br.ReadBytes(500000);
    br.Close();
}
responseStream.Close();
imageResponse.Close();

FileStream fs = new FileStream(saveLocation, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
try
{
    bw.Write(imageBytes);
}
finally
{
    fs.Close();
    bw.Close();
}

OTHER TIPS

You can save it through web client as:

WebClient webClient = new WebClient();
webClient.DownloadFile(ImageFileUrl, localFileName);

See this article:

Instead of the StreamWriter use the BinaryWriter to write bytes.

Try this

 string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
 string filePath = Path.Combine(path, "filename.jpg");
  using (IsolatedStorageFileStream fStream = new IsolatedStorageFileStream(filePath, FileMode.Create, isoFile))
  {
      yourFileStream.CopyTo(fStream);

      //OR

      fStream.Write(yourFileStream.GetBytes(), 0, yourFileStream.GetBytes().Length);
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top