private static Stream GenerateImage(Vision vision, string penColor, string fillColor, Stream fileStream) { var accentColor = ColorFromHex(vision.color.accentColor); var borderColor = ContrastColor(accentColor); var rectColor = new Color(); if (!string.IsNullOrEmpty(penColor)) { borderColor = ColorFromHex(penColor); } if (!string.IsNullOrEmpty(fillColor)) { rectColor = ColorFromHex(fillColor); } var image = Image.FromStream(fileStream); using (var g = Graphics.FromImage(image)) { foreach (var face in vision.faces) { var x = face.faceRectangle.left; var y = face.faceRectangle.top; var width = face.faceRectangle.width; var height = face.faceRectangle.height; if (!string.IsNullOrEmpty(penColor)) { var rectangle = new Rectangle(x, y, width, height); var pen = new Pen(borderColor, 2) { Alignment = PenAlignment.Outset }; g.DrawRectangle(pen, rectangle); } if (!string.IsNullOrEmpty(fillColor)) { var rectangleF = new RectangleF(x, y, width, height); var customColor = Color.FromArgb(50, rectColor); var shadowBrush = new SolidBrush(customColor); g.FillRectangles(shadowBrush, new[] {rectangleF}); } } } var stream = ToStream(image, ImageFormat.Jpeg); return stream; }
public async Task<Vision> PostFormData() { if (!Request.Content.IsMimeMultipartContent("form-data")) { var emptyVision = new Vision(); return emptyVision; } var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider()); var files = provider.Files; var file1 = files[0]; var fileStream = await file1.ReadAsStreamAsync(); var keys = Request.RequestUri.ParseQueryString(); var generateImage = true;//keys["generateimage"] == "1"; var penColor = "123123";//keys["pencolor"]; var fillColor = "231231";//keys["fillcolor"]; var extension = ExtractExtension(file1); var contentType = file1.Headers.ContentType.ToString(); var imageName = string.Concat(Guid.NewGuid().ToString(), extension); var imageNameWithRect = string.Concat(Guid.NewGuid().ToString(), "-rect", extension); var blobUri = UploadToBlobStorage(fileStream, imageName, contentType); var vision = await GetVisionFromImage(blobUri); if (generateImage) { var stream = GenerateImage(vision, penColor, fillColor, fileStream); vision.imageUrl = UploadToBlobStorage(stream, imageNameWithRect, contentType).ToString(); } return vision; }