2013-10-29 62 views
0

我要創造我自己的動態橫幅動態橫幅,於是我開始製作圖片處理程序,ATM我有這樣的代碼:創建圖像處理程序

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace Faeria 
{ 
/// <summary> 
/// Summary description for FaeriaImage 
/// </summary> 
public class FaeriaImage : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "image/jpeg"; 
     context.Response.Write("~/Images/bg1.jpg"); 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

}

但是當我打電話「 http:// localhost:12361/FaeriaImage.ashx「我只得到這個:http://abload.de/image.php?img=1deib0.jpg

當我在我的網站中調用它時,我沒有任何圖像。

這是什麼錯誤?

回答

1

我已經使用過處理程序,據我所知,您需要在處理程序中繪製圖像。這段代碼可以幫助你,因爲它幫助了我。試試吧

using (Bitmap image = new Bitmap(context.Server.MapPath("~/Images/bg1.jpg"))) 
{ 
    using(MemoryStream ms = new MemoryStream()) 
    { 
     image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
     ms.WriteTo(context.Response.OutputStream); 
    } 
} 
0

更改您的代碼如下(不會寫,但WriteFile的)

public void ProcessRequest(HttpContext context) 
{ 
    context.Response.ContentType = "image/jpeg"; 
    context.Response.WriteFile("~/Images/bg1.jpg"); 
}