如何将图像保存到隔离存储

时间:2012-10-13 17:41:16

标签: c# wpf image isolatedstoragefile

我想从网址下载图像,然后将其作为图像文件保存在隔离存储中。我已经保存了一些字符串值,但我不知道如何保存图像文件。谢谢!

4 个答案:

答案 0 :(得分:2)

您也可以通过二进制编写器执行;

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

答案 1 :(得分:1)

您可以通过网络客户端将其保存为:

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

答案 2 :(得分:0)

请参阅this article

而不是StreamWriter使用BinaryWriter来写字节。

答案 3 :(得分:0)

试试这个

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