2013-07-22 32 views
0

我想直接顯示從webresponse瀏覽器的圖像,而不保存到文件。我使用下面的代碼將圖像保存到文件,但我想直接在瀏覽器中顯示。該網站提供了我想要的web反應中的驗證碼圖像來display.please幫助我。如何直接從asp.net中的httpwebresponse流顯示圖像?

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(); 
} 
+0

請更詳細地指出代碼的錯誤或錯誤發生的位置? –

+0

請檢查您的圖片保存路徑權限。 –

回答

1
  • 您可以使用HttpResponse.WriteFile方法來流直接發送到客戶端。 將指定的文件直接寫入HTTP響應輸出流。

  • 您可以創建IHttpHandler來發送流,從而避免一些頁面生命週期,提高性能。 Here is the link