user1005633
user1005633

Reputation: 755

How to save image to Isolate Storage

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!

Upvotes: 0

Views: 1065

Answers (4)

codingbiz
codingbiz

Reputation: 26386

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

Upvotes: 0

user1711092
user1711092

Reputation:

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

Upvotes: 2

Emond
Emond

Reputation: 50672

See this article:

Instead of the StreamWriter use the BinaryWriter to write bytes.

Upvotes: 0

user1711092
user1711092

Reputation:

You can save it through web client as:

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

Upvotes: 1

Related Questions