zoukankan      html  css  js  c++  java
  • ASP.NET下载远程图片保存到本地的方法、保存抓取远程图片

    以下介绍两种方法:
    1.利用WebRequest,WebResponse 类

    WebRequest wreq=WebRequest.Create("http://up.2cto.com/2012/0516/20120516112717995.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://up.2cto.com/2012/0516/20120516112717995.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);
    //如果是真实的图片地址直接用
       my.DownloadFile("http://up.2cto.com/2012/0516/20120516112717995.gif","D:\a.gif");   //直接可以保存


  • 相关阅读:
    bzoj 2527: [Poi2011]Meteors 整体二分
    bzoj 2738 矩阵乘法
    bzoj 3110 K大数查询
    bzoj 3262 陌上花开
    cogs 577 蝗灾 CDQ分治
    bzoj 1101 zap
    bzoj 2005
    bzoj 3518 Dirichlet卷积
    bzoj 1257
    最优贸易 [NOIP 2009]
  • 原文地址:https://www.cnblogs.com/hnsongbiao/p/4456992.html
Copyright © 2011-2022 走看看