C# 获取外网图片并保存到相对应路径
来源:互联网 发布:烟台网络党校行政学院 编辑:程序博客网 时间:2024/05/25 19:58
public static int SaveImageFromWeb(string imgUrl,string path,string fileName)
...{
if(path.Equals(""))
throw new Exception("未指定保存文件的路径");
string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("/")+1);
string defaultType = ".jpg";
string[] imgTypes = new string[]...{".jpg",".jpeg",".png",".gif",".bmp"};
string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("."));
foreach (string it in imgTypes)
...{
if (imgType.ToLower().Equals(it))
break;
if (it.Equals(".bmp"))
imgType = defaultType;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
request.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)";
request.Timeout = 3000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
if( response.ContentType.ToLower().StartsWith("image/") )
...{
byte[] arrayByte = new byte[1024];
int imgLong = (int)response.ContentLength;
int l = 0;
if(fileName == "")
fileName = imgName;
FileStream fso = new FileStream(path+fileName+imgType,FileMode.Create);
while(l<imgLong)
...{
int i = stream.Read(arrayByte,0,1024);
fso.Write(arrayByte,0,i);
l += i;
}
fso.Close();
stream.Close();
response.Close();
return 1;
}
else
...{
return 0;
}
}
...{
if(path.Equals(""))
throw new Exception("未指定保存文件的路径");
string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("/")+1);
string defaultType = ".jpg";
string[] imgTypes = new string[]...{".jpg",".jpeg",".png",".gif",".bmp"};
string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("."));
foreach (string it in imgTypes)
...{
if (imgType.ToLower().Equals(it))
break;
if (it.Equals(".bmp"))
imgType = defaultType;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
request.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)";
request.Timeout = 3000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
if( response.ContentType.ToLower().StartsWith("image/") )
...{
byte[] arrayByte = new byte[1024];
int imgLong = (int)response.ContentLength;
int l = 0;
if(fileName == "")
fileName = imgName;
FileStream fso = new FileStream(path+fileName+imgType,FileMode.Create);
while(l<imgLong)
...{
int i = stream.Read(arrayByte,0,1024);
fso.Write(arrayByte,0,i);
l += i;
}
fso.Close();
stream.Close();
response.Close();
return 1;
}
else
...{
return 0;
}
}
- C# 获取外网图片并保存到相对应路径
- C#获取相对路径
- C#获取相对路径
- c# 获取相对路径
- c# 获取相对路径
- c# 获取相对路径
- c# 获取相对路径
- c# 获取相对路径
- c# 获取相对路径
- c# 获取相对路径
- C#获取相对路径
- c# 获取相对路径
- iOS保存图片到本地,获取路径
- 获取网络图片,并保存到本地
- 加载上传图片,获取文件名、文件路径并保存到程序的文件夹中
- C#获取相对路径&获取当前路径
- C#获取文件相对路径
- iOS 把图片保存到相册,并获取图片文件名
- 11个经典智力题,看你会做几个
- ASP.NET跨页面传值技巧
- asp.net学习笔记
- cmmi配置管理
- 语音技术,应用的变迁
- C# 获取外网图片并保存到相对应路径
- null
- 浅谈ASP.NET中弹出窗口技术
- 如何取得網路上的圖片
- Asp.net上传图片并生成缩略图
- 怎么在项目开发过程控制开发人员的开发约束?
- ASP.NET 常见参考项目的 UI、BLL 、Model 、 DAL 分析
- 利用VS2005中的Code Snippets提高开发效率
- XReport报表工具0.6版本发布