2011-08-13 22 views
0

我已經戲劇性地改變了這個問題,但它在這裏。我正在從IP攝像機讀取mjpeg流,一切正常。但現在在我接收到的每一幀中(請參閱stream_NewFrame),我想將此圖像推送給客戶端。但我似乎無法弄清楚如何訪問HttpContext.Current導致它在該函數中始終爲空。有沒有人知道如何訪問HttpContext上下文,就像我可以在ProcessRequest函數中一樣?我想在這裏我錯過了一些明顯的東西,但我無法弄清楚什麼!感謝您的時間。ashx處理程序,訪問HttpContext.Current裏面的空洞

public class ImageHandler : IHttpHandler, IRequiresSessionState 
{ 

    public void ProcessRequest(HttpContext context) 
    { 

     //Get parameter 
     string Url = context.Request.QueryString["url"]; 
     string Username = context.Request.QueryString["username"]; 
     string Password = context.Request.QueryString["password"]; 

     //Set cache 
     HttpResponse Response = HttpContext.Current.Response; 
     Response.Expires = 0; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = "multipart/x-mixed-replace"; 

     // create MJPEG video source 
     MJPEGStream stream = new MJPEGStream(string.Format("{0}/video.cgi?user={1}&pwd={2}", Url, Username, Password)); 
     stream.NewFrame += new NewFrameEventHandler(stream_NewFrame); 
     stream.Start(); 

    } 


    private void stream_NewFrame(object sender, NewFrameEventArgs eventArgs) 
    { 
     Image img = eventArgs.Frame; 
     byte[] b = GetImageBytes(eventArgs.Frame); 
     HttpContext.Current.Response.OutputStream.Write(b, 0, b.Length); 

    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 

} 

回答

1

你爲什麼不只是保存HttpContext遠sometwhere所以它從stream_NewFrame方法訪問?我會建議在你的類中使用一個成員變量。

如果您想要封裝更多的內容,請創建一個單獨的類,將HttpContext供入,然後將stream_NewFrame方法替換爲該類。喜歡的東西:

class Processor 

     private HttpContext _context; 

     public Processor(HttpContext context) { 
      _context = context; 
     } 

     public void stream_NewFrame(object sender, NewFrameEventArgs eventArgs) 
     { 
      Image img = eventArgs.Frame; 
      byte[] b = GetImageBytes(eventArgs.Frame); 
      HttpContext.Current.Response.OutputStream.Write(b, 0, b.Length); 
     } 
} 

,然後在你的ProcessRequest,你這樣做:

公共類ImageHandler:IHttpHandler的,IRequiresSessionState {

public void ProcessRequest(HttpContext context) 
{ 

    //Get parameter 
    string Url = context.Request.QueryString["url"]; 
    string Username = context.Request.QueryString["username"]; 
    string Password = context.Request.QueryString["password"]; 

    //Set cache 
    HttpResponse Response = HttpContext.Current.Response; 
    Response.Expires = 0; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = "multipart/x-mixed-replace"; 

    // create processor 
    Processor p = new Processor(context);  

    // create MJPEG video source 
    MJPEGStream stream = new MJPEGStream(string.Format("{0}/video.cgi?user={1}&pwd={2}", Url, Username, Password)); 
    stream.NewFrame += new NewFrameEventHandler(p.stream_NewFrame); 
    stream.Start(); 

} 
0

MJPEGStream創建一個後臺線程,它運行在。 HTTPContext.Current是一個線程局部變量,這意味着後臺線程(這是調用stream_newFrame回調函數的線程)在不同的線程上,所以它不具有相同的HTTPContext(實際上它沒有)。您需要以其他方式提供。創建一個處理器對象來保存像Erik這樣的引用的想法應該很好。