我想测试一个解决方案:从本地摄像头捕获图像数据,然后显示!
这是我的控制者:
public class DevController : Controller
{
public byte[] Pixels = null;
Bitmap _bmp = new Bitmap(480, 272, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics _g = null;
System.Timers.Timer _timer = new System.Timers.Timer(1000);
public ActionResult Index()
{
if (_g == null)
_g = Graphics.FromImage(_bmp);
_g.Clear(Color.Black);
_g.FillRectangle(Brushes.Blue, 2, 2, 65, 31);
_g.DrawString("hello ", new Font("Arial", 15f), Brushes.Yellow, new PointF(5f, 5f));
MemoryStream ms = new MemoryStream();
_bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
Pixels = ms.ToArray();
_timer.AutoReset = true;
_timer.Enabled = true;
_timer.Elapsed += _timer_Elapsed;
return View(this);
}
}
使用Timer更新模型数据:
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (_g == null)
{
_g = Graphics.FromImage(_bmp);
}
_g.Clear(Color.Black);
_g.FillRectangle(Brushes.Red, 2, 2, 65, 31);
_g.DrawString(string.Format("hello :{0}", id++), new Font("Arial", 15f), Brushes.Yellow, new PointF(5f, 5f));
MemoryStream ms = new MemoryStream();
_bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
Pixels = ms.ToArray();
}
查看代码
<img src="data:image/bmp;base64,@System.Convert.ToBase64String(Model.Pixels)" id="u_image" />
问题是:
当Pixels
在时间发生变化时,视图如何自动更改?