how to display image directly from httpwebresponse stream in asp.net?

北战南征 提交于 2019-12-13 06:12:02

问题


I want to display image recieved from webresponse to browser directly without saving to a file.i have used below code to save the image to file but i want to display in browser directly.the website provides the captcha image in webresponse which i want to display.please help me.

public void captcha(string id, string pass)
{
    HttpWebRequest req;
    HttpWebResponse response;
    string strNewValue,ckuser,ckpass;
    System.Drawing.Image returnval;
    ckuser = id;
    ckpass = pass;
    this.req = (HttpWebRequest)WebRequest.Create("http://site2sms.com/security/captcha.asp");
    ServicePointManager.Expect100Continue = false;
    this.req.CookieContainer = new CookieContainer();
    this.req.AllowAutoRedirect = false;
    this.req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0";
    this.req.Accept = "*/*";
    this.req.Method = "POST";
    this.req.CookieContainer = cookieCntr;
    this.req.ContentType = "application/x-www-form-urlencoded";
    this.req.Referer = "http://site2sms.com/verification.asp?source=login";
    this.strNewValue = "user=" + ckuser + "&password=" + ckpass + "&Submit=Sign+in";
    this.req.ContentLength = this.strNewValue.Length;
    StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
    writer.Write(this.strNewValue);
    writer.Close();
    this.response = (HttpWebResponse)this.req.GetResponse();
    returnval = Image.FromStream(response.GetResponseStream());
   // returnval.Save( Server.MapPath("captcha.bmp"));
    Response.AppendHeader("Content-Disposition:", "inline;filename=captcha.bmp");
    Response.ContentType = "image/bmp";
    Response.Write(returnval);
    Response.End();
    this.response.Close();
 }

回答1:


  • You can use HttpResponse.WriteFile method to send the stream to client directly. Writes the specified file directly to an HTTP response output stream.

  • You can create the IHttpHandler to send the stream, thus avoid some page life circle, increase the performance. Here is the link



来源:https://stackoverflow.com/questions/17780666/how-to-display-image-directly-from-httpwebresponse-stream-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!