Search code examples
c#wpfimageisolatedstoragefile

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!


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