2012-10-13 57 views
0

我想從網址下載圖像,然後將其作爲圖像文件保存在隔離存儲器中。我已經保存了一些字符串值,但我不知道如何保存圖像文件。謝謝!相反如何將圖像保存到隔離存儲器

的StreamWriter的的使用BinaryWriter寫字節:

+0

那你試試?保存圖像與保存任何其他類型的文件完全相同 –

+0

爲了保存一個字符串,我創建了一個新的StreamWriter,然後使用方法WriteLine將其寫入文件。用圖像我應該怎麼做? – user1005633

回答

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

謝謝它適合我! – user1005633

1

您可以通過Web客戶端將其保存爲:

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

是的,但用這種方式它將文件保存在路徑中(我必須給它)。我想保存到隔離存儲... – user1005633

+0

r你想使用文件流? – 2012-10-13 18:08:55

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); 
    } 
相關問題