以下介紹兩種方法:
1.利用WebRequest,WebResponse 類
WebRequest wreq=WebRequest.Create("http://www.xueit.com/eimg/uploadfile/downpig/20098/098215331763.gif");
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); //保存
//下面直接輸出
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("http://www.x .com/eimg/uploadfile/downpig/20098/098215331763.gif");
MemoryStream ms=new MemoryStream(mybyte);
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(ms);
img.Save("D:\\a.gif",ImageFormat.Gif); //保存
//下面直接輸出
Response.ClearContent();
Response.ContentType="image/gif";
Response.BinaryWrite(mybyte);
如果是真實(shí)的圖片地址直接用
my.DownloadFile("http://www. /eimg/uploadfile/downpig/20098/098215331763.gif","D:\\a.gif"); //保存
直接可以保存
該文章在 2020/3/3 1:35:36 編輯過