用ASP.NET实现下载远程图片保存到本地的方法 保存
网站建设 2021-07-03 08:43www.dzhlxh.cn网站建设
以下介绍两种方法:
1.利用WebRequest,WebResponse 类
WebRequest wreq=WebRequest.Create("");
HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
Stream s=wresp.GetResponseStream();
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(s);
img.Save("D:\\aa.gif",ImageFormat.Gif); //保存
//狼蚁网站SEO优化直接输出
MemoryStream ms=new MemoryStream();
img.Save(ms,ImageFormat.Gif);
img.Dispose();
Response.ClearContent();
Response.ContentType="image/gif";
Response.BinaryWrite(ms.ToArray());
2.利用 WebClient 类
WebClient my=new WebClient();
byte[] mybyte;
mybyte=my.DownloadData(""); MemoryStream ms=new MemoryStream(mybyte);
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(ms);
img.Save("D:\\a.gif",ImageFormat.Gif); //保存
//狼蚁网站SEO优化直接输出
Response.ClearContent();
Response.ContentType="image/gif";
Response.BinaryWrite(mybyte);
如果是真实的图片地址直接用
my.DownloadFile(""); //保存
直接可以保存
1.利用WebRequest,WebResponse 类
WebRequest wreq=WebRequest.Create("");
HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
Stream s=wresp.GetResponseStream();
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(s);
img.Save("D:\\aa.gif",ImageFormat.Gif); //保存
//狼蚁网站SEO优化直接输出
MemoryStream ms=new MemoryStream();
img.Save(ms,ImageFormat.Gif);
img.Dispose();
Response.ClearContent();
Response.ContentType="image/gif";
Response.BinaryWrite(ms.ToArray());
2.利用 WebClient 类
WebClient my=new WebClient();
byte[] mybyte;
mybyte=my.DownloadData(""); MemoryStream ms=new MemoryStream(mybyte);
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(ms);
img.Save("D:\\a.gif",ImageFormat.Gif); //保存
//狼蚁网站SEO优化直接输出
Response.ClearContent();
Response.ContentType="image/gif";
Response.BinaryWrite(mybyte);
如果是真实的图片地址直接用
my.DownloadFile(""); //保存
直接可以保存