protected string GetInlineImageWithIdentifiedFaces(IEnumerable <IdentifiedFace> faces) { ImageToProcess.Seek(0, SeekOrigin.Begin); using (var img = new Bitmap(ImageToProcess)) // make copy, drawing on indexed pixel format image is not supported using (var nonIndexedImg = new Bitmap(img.Width, img.Height)) using (var g = Graphics.FromImage(nonIndexedImg)) using (var mem = new MemoryStream()) { g.DrawImage(img, 0, 0, img.Width, img.Height); var i = 0; foreach (var face in faces) { var pen = new Pen(face.Color, 5); var faceRectangle = face.Face.FaceRectangle; var rectangle = new Rectangle(faceRectangle.Left, faceRectangle.Top, faceRectangle.Width, faceRectangle.Height); g.DrawRectangle(pen, rectangle); i++; } nonIndexedImg.Save(mem, ImageFormat.Png); var base64 = Convert.ToBase64String(mem.ToArray()); return(String.Format("data:image/png;base64,{0}", base64)); } }
protected string GetInlineImageWithLines(HandwritingTextResult result) { ImageToProcess.Seek(0, SeekOrigin.Begin); using (var img = new Bitmap(ImageToProcess)) // make copy, drawing on indexed pixel format image is not supported using (var nonIndexedImg = new Bitmap(img.Width, img.Height)) using (var g = Graphics.FromImage(nonIndexedImg)) using (var mem = new MemoryStream()) { g.DrawImage(img, 0, 0, img.Width, img.Height); var i = 0; foreach (var line in result.Lines) { var pen = new Pen(Settings.ImageSquareColors[i], 5); var points = line.Polygon.Points.Select(pp => new System.Drawing.Point { X = pp.X, Y = pp.Y }).ToArray(); g.DrawPolygon(pen, points); i++; } nonIndexedImg.Save(mem, ImageFormat.Png); var base64 = Convert.ToBase64String(mem.ToArray()); return(String.Format("data:image/png;base64,{0}", base64)); } }
public async Task <ActionResult> Landmarks() { ViewBag.Title = "Face landmarks"; if (Request.HttpMethod == "GET") { return(View("Index")); } var imageResult = ""; var file = Request.Files[0]; Face[] faces = new Face[] { }; await RunOperationOnImage(async stream => { faces = await FaceClient.DetectAsync(stream, returnFaceLandmarks: true); }); ImageToProcess.Seek(0, SeekOrigin.Begin); using (var img = new Bitmap(ImageToProcess)) // make copy, drawing on indexed pixel format image is not supported using (var nonIndexedImg = new Bitmap(img.Width, img.Height)) using (var g = Graphics.FromImage(nonIndexedImg)) using (var mem = new MemoryStream()) { g.DrawImage(img, 0, 0, img.Width, img.Height); var pen = new Pen(Color.Red, 5); foreach (var face in faces) { var props = typeof(FaceLandmarks).GetProperties(); foreach (var prop in props) { if (prop.PropertyType == typeof(FeatureCoordinate)) { var coordinate = (FeatureCoordinate)prop.GetValue(face.FaceLandmarks); var rect = new Rectangle((int)coordinate.X, (int)coordinate.Y, 2, 2); g.DrawRectangle(pen, rect); } } } nonIndexedImg.Save(mem, ImageFormat.Png); var base64 = Convert.ToBase64String(mem.ToArray()); imageResult = String.Format("data:image/png;base64,{0}", base64); } return(View("Index", (object)imageResult)); }
protected string GetInlineImageWithLines(OcrResults result) { ImageToProcess.Seek(0, SeekOrigin.Begin); using (var img = new Bitmap(ImageToProcess)) // make copy, drawing on indexed pixel format image is not supported using (var nonIndexedImg = new Bitmap(img.Width, img.Height)) using (var g = Graphics.FromImage(nonIndexedImg)) using (var mem = new MemoryStream()) { g.DrawImage(img, 0, 0, img.Width, img.Height); var i = 0; foreach (var region in result.Regions) { foreach (var line in region.Lines) { var pen = new Pen(Settings.ImageSquareColors[i], 2); g.DrawRectangle(pen, new System.Drawing.Rectangle( line.Rectangle.Left, line.Rectangle.Top, line.Rectangle.Width, line.Rectangle.Height )); i++; if (i >= 10) { i = 0; } } } nonIndexedImg.Save(mem, ImageFormat.Png); var base64 = Convert.ToBase64String(mem.ToArray()); return(String.Format("data:image/png;base64,{0}", base64)); } }