我已经极大地改变了这个问题,但它就在这里。我正在从 IP cam 读取 mjpeg 流,一切正常。但是现在在我收到的每一帧(参见 stream_NewFrame)上,我想将此图像推送到客户端。但我似乎无法弄清楚如何访问 HttpContext.Current 因为它在该函数中始终为空。有谁知道如何访问 HttpContext 上下文,就像我可以在 ProcessRequest 函数中一样?我想我在这里遗漏了一些明显的东西,但我不知道是什么!感谢您的时间。
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;
}
}
}