NET MVC,现在我的情况是,在我的控制器中,当点击一个按钮时,我正在创建一个局部视图,并且视图的模型包含一个监听事件的类。
StreamModel streamModel = m_StreamModel = new StreamModel(cameraId, rgsLiveVideoWebCall);
string view = RenderRazorViewToString("ImageView", streamModel);
public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View,
ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
StreamModel类如下所示:
public class StreamModel
{
public StreamHolder StreamHolder { get; set; }
public string Image { get; set; }
public StreamModel(string cameraId, string url)
{
StreamHolder = new StreamHolder(cameraId, url);
}
}
和StreamHolder持有者类如下:
public class StreamHolder
{
private object GetNewImageLock = new object();
public string CameraId { get; set; }
private MJPEGStream m_MJPEGStream;
public byte[] CurrentImageBytes { get; set; }
public string ImageBase64 { get; set; }
public StreamHolder(string cameraId, string url)
{
CameraId = cameraId;
CurrentImageBytes = null;
ImageBase64 = null;
lock (this)
{
m_MJPEGStream = new MJPEGStream(url);
m_MJPEGStream.RequestTimeout = 100000;
m_MJPEGStream.ForceBasicAuthentication = true;
m_MJPEGStream.Login = "admin";
m_MJPEGStream.Password = "admin";
m_MJPEGStream.NewFrame += stream_NewFrame;
m_MJPEGStream.PlayingFinished += stream_PlayingFinished;
m_MJPEGStream.VideoSourceError += stream_VideoSourceError;
m_MJPEGStream.Start();
}
}
public void StopStreaming()
{
lock (this)
{
if (m_MJPEGStream != null)
{
try
{
m_MJPEGStream.Stop();
m_MJPEGStream.WaitForStop();
m_MJPEGStream.NewFrame -= stream_NewFrame;
m_MJPEGStream.PlayingFinished -= stream_PlayingFinished;
m_MJPEGStream.VideoSourceError -= stream_VideoSourceError;
m_MJPEGStream = null;
}
catch { }
}
}
}
private void stream_VideoSourceError(object sender, VideoSourceErrorEventArgs eventArgs)
{
StopStreaming();
}
private void stream_PlayingFinished(object sender, ReasonToFinishPlaying reason)
{
StopStreaming();
}
private void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
lock (GetNewImageLock)
{
if (HomeController.m_LastImageRequest != null && (DateTime.Now - HomeController.m_LastImageRequest).TotalSeconds > 10)
{
StopStreaming();
return;
}
if (eventArgs.Frame != null)
{
CurrentImageBytes = BitmapToBytes(eventArgs.Frame);
var base64 = Convert.ToBase64String(CurrentImageBytes);
ImageBase64 = String.Format("data:image/gif;base64,{0}", base64);
}
}
}
// This method is for converting bitmap into a byte array
private static byte[] BitmapToBytes(Bitmap img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
}
我有两个主要问题: 1)我可能有多个此模型的实例,我需要在创建它们后访问和管理它们。 2)当标签/窗口关闭时,我需要正确处理它们。
答案 0 :(得分:0)
你可以有一个
private static List<StreamModel> streamModels = new List<StreamModel>();
在你的一个课程中。您可以定义一个方法,如下所示:
public static StreamModel generateStreamModel(string cameraid, string url) {
StreamModel streamModel = new StreamModel(cameraid, url);
streamModels.Add(streamModel);
return streamModel;
}
要处置实例,请将其从streamModels
中删除。如果它未被引用,则应由垃圾收集器收集。