0

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

4

4 回答 4

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();
}
于 2012-10-13T18:18:35.123 回答
1

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

WebClient webClient = new WebClient();
webClient.DownloadFile(ImageFileUrl, localFileName);
于 2012-10-13T17:51:51.547 回答
0

这篇文章

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

于 2012-10-13T18:13:19.830 回答
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);
  }
于 2012-10-13T18:22:07.440 回答