Frage

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();
 }
War es hilfreich?

Lösung

  • 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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top