C#下在网络图片到本地磁盘
项目中用到下载图片代码,百度了一下记录如下:
// 从图片地址下载图片到本地磁盘 // 将二进制文件保存到磁盘 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Net; using System.IO; using System.Text; private void menuItem36_Click(object sender, System.EventArgs e) { string FileName; string Url; FileName="c://1.gif"; Url="http://www.baidu.com/img/logo-yy.gif"; if (SavePhotoFromUrl(FileName,Url)){ MessageBox.Show("图片下载成功"); } else { MessageBox.Show("图片下载失败"); } } /// <summary> /// 从图片地址下载图片到本地磁盘 /// </summary> /// <param name="ToLocalPath">图片本地磁盘地址</param> /// <param name="Url">图片网址</param> /// <returns></returns> public static bool SavePhotoFromUrl(string FileName,string Url) { bool Value=false; WebResponse response = null; Stream stream = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); response = request.GetResponse(); stream = response.GetResponseStream(); if( !response.ContentType.ToLower().StartsWith("text/") ) { Value=SaveBinaryFile(response,FileName); } } catch(Exception err) { string aa=err.ToString(); } return Value; } /// <summary> /// Save a binary file to disk. /// </summary> /// <param name="response">The response used to save the file</param> // 将二进制文件保存到磁盘 private static bool SaveBinaryFile(WebResponse response,string FileName) { bool Value=true; byte []buffer = new byte[1024]; try { if(File.Exists(FileName)) File.Delete(FileName); Stream outStream =System.IO.File.Create( FileName ); Stream inStream = response.GetResponseStream(); int l; do { l = inStream.Read(buffer,0,buffer.Length); if(l>0) outStream.Write(buffer,0,l); } while(l>0); outStream.Close(); inStream.Close(); } catch { Value=false; } return Value; }
猜您可能还喜欢
- asp.net mvc内微信pc端、H5、JsApi支付方式总结(5702)
- HTML5 WebSocket与C#建立Socket连接实现代码(2866)
- asp.net mvc开发移动端省、市、县三级地区选择控件实现方法(1700)
- C# 使用Socket链接Ftp服务器下载上传代码FTPClient(1634)
- VS 2017调试程序鼠标定位文本输入框浏览器闪退调试终止解决方法(1487)
- C# 两个类的实例之间相同属性的值的复制(1380)
- 关于“System.Data.OleDb.OleDbException,外部数据库驱动程序 (1) 中的意外错误。”的解决方案(1336)
- C#微信公众号推送消息接口消息排重(1175)
- html5 webScoket与C#建立Socket连接(1074)
- 什么是.NET Core ?它和.NET Framework 有什么不同?(1047)
评论列表
发表评论
文章分类
文章归档
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
阅读排行
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5702)
- 2.各大搜索网站网站收录提交入口地址(3201)
- 3.Windows 10休眠文件更改存储位置(3164)
- 4.ECharts仪表盘实例及参数使用详解(3095)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(2993)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(2866)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(2787)
- 8.CERT_HAS_EXPIRED错误如何解决(2250)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2189)
- 10.HBuilder编辑器格式化代码(2118)