原理:调出摄像头,拍照保存到画布,将图片URi传送到后台
前端代码(Index.cshtml):
后台代码(HomeController.cs):
using System;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Web.Mvc;namespace WebFrame.Controllers{ public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(FormCollection form) { try { byte[] imgBytes = Convert.FromBase64String(form['img']); Stream stream = new MemoryStream(imgBytes); Image imgae = Image.FromStream(stream); imgae.Save(HttpContext.Server.MapPath('~/') + Guid.NewGuid().ToString() + '.jpg', ImageFormat.Jpeg); return Content('yes'); } catch (Exception e) { return Content(e.Message); } } }}
效果如下图: