【问题标题】:How to save image to Isolate Storage如何将图像保存到隔离存储
【发布时间】:2012-10-13 17:41:15
【问题描述】:

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

【问题讨论】:

  • 你尝试了什么?保存图像与保存任何其他类型的文件完全相同
  • 为了保存字符串,我创建了一个新的 StreamWriter,然后使用方法 WriteLine 将其写入文件。有了图片我该怎么办?

标签: c# wpf image isolatedstoragefile


【解决方案1】:

你也可以通过二进制编写器来做到这一点;

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 是的,但是通过这种方式,它将文件保存在路径中(我必须给它)。我想把它保存到独立存储...
    【解决方案3】:

    this article:

    使用BinaryWriter 代替 StreamWriter 来写入字节。

    【讨论】:

      【解决方案4】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-25
        相关资源
        最近更新 更多