下载远程图片和展示图片

protected void Page_Load(object sender, EventArgs e)

{

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"http://www.baidu.com/img/bdlogo.gif");

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();



    using (Stream s = response.GetResponseStream())

    {

        //#region 转换方式一  通过绘制方式

        //byte[] bytes = new byte[1024 * 1024];

        //int count = s.Read(bytes, 0, bytes.Length);

        //MemoryStream ms = new MemoryStream(bytes, 0, count);

        //System.Drawing.Image img = new Bitmap(ms);//内存流生成图片,图片也可以生成内存流输入到前台

        //img.Save(@"C:\百度.jpg",ImageFormat.Jpeg);

        //#endregion



        //#region 方法二 通过文件流方式保存

        //using (FileStream fs = new FileStream(@"C:\baidu.jpg",FileMode.Create))

        //{

        //    byte[] bytes = new byte[10 * 1024 * 1024];

        //    int count = s.Read(bytes, 0, bytes.Length);//byte流生成文件

        //    fs.Write(bytes, 0, count);

        //}

        //#endregion



        #region 通过内存流输入到前台

        byte[] bytes = new byte[10 * 1024 * 1024];

        int count = s.Read(bytes, 0, bytes.Length);

        MemoryStream ms = new MemoryStream(bytes);



        Response.ClearContent();

        Response.ContentType = "image/gif";

        Response.BinaryWrite(ms.ToArray());

        #endregion

    }

}

  

你可能感兴趣的:(下载)