C# .NET 根据Url链接保存Image图片到本地磁盘
根據一個Image的Url鏈接可以在瀏覽器中顯示一個圖片,如果要通過代碼將圖片保存在本地磁盤可以通過以下方式:
1、首先獲取圖片的二進制數組。
?static public byte[] GetBytesFromUrl(string url)
? {
?? ? ? byte[] b;
??? ?? HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
???? ? WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}
?? 2、保存到磁盤文件中.
?static public void WriteBytesToFile(string fileName, byte[] content)
?{
???? ?? FileStream fs = new FileStream(fileName, FileMode.Create);
???? ?? BinaryWriter w = new BinaryWriter(fs);
???? ? try
? ? ? {
????? ?? w.Write(content);
????? }
????? finally
? ?? {
?? ???? fs.Close();
?? ???? w.Close();
? ? }
???? ? }
posted on 2018-04-29 00:50 NET未來之路 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/lonelyxmas/p/8970176.html
總結
以上是生活随笔為你收集整理的C# .NET 根据Url链接保存Image图片到本地磁盘的全部內容,希望文章能夠幫你解決所遇到的問題。