public async Task <ActionResult> Index() { if (Request.HttpMethod == "GET") { return(View("Index")); } var model = new DescribeImageModel(); var features = new[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags }; await RunOperationOnImage(async stream => { model.Result = await VisionServiceClient.AnalyzeImageAsync(stream, features); }); await RunOperationOnImage(async stream => { var bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); var base64 = Convert.ToBase64String(bytes); model.ImageDump = String.Format("data:image/png;base64,{0}", base64); }); return(View(model)); }
public async Task <ActionResult> Index(HttpPostedFileBase file) { if (Request.HttpMethod == "GET") { return(View("Index")); } var model = new DescribeImageModel(); var features = new[] { VisualFeature.Adult, VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.Faces, VisualFeature.ImageType, VisualFeature.Tags }; await RunOperationOnImage(async stream => { model.Result = await VisionServiceClient.AnalyzeImageAsync(stream, features); }); await RunOperationOnImage(async stream => { var bytes = new byte[stream.Length]; await stream.ReadAsync(bytes, 0, bytes.Length); var base64 = Convert.ToBase64String(bytes); model.ImageDump = String.Format("data:image/png;base64,{0}", base64); }); if (file != null && file.ContentLength > 0) { if (!file.ContentType.StartsWith("image")) { TempData["Message"] = "Only image files may be uploaded"; } else { try { CloudStorageAccount account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); CloudBlobClient client = account.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference("pictures"); CloudBlockBlob photo = container.GetBlockBlobReference(Path.GetFileName(file.FileName)); await photo.UploadFromStreamAsync(file.InputStream); using (var outputStream = new MemoryStream()) { file.InputStream.Seek(0L, SeekOrigin.Begin); var settings = new ResizeSettings { MaxWidth = 192 }; ImageBuilder.Current.Build(file.InputStream, outputStream, settings); outputStream.Seek(0L, SeekOrigin.Begin); container = client.GetContainerReference("pictures"); CloudBlockBlob thumbnail = container.GetBlockBlobReference(Path.GetFileName(file.FileName)); await thumbnail.UploadFromStreamAsync(outputStream); } } catch (Exception ex) { // In case something goes wrong have to be dumb to get this TempData["Message"] = ex.Message; } } } // var imageUrl = await imageService.UploadImageAsync(ImageToAnalyze); // TempData["LatestImage"] = imageUrl.ToString(); //var latestImage = string.Empty; //if (TempData["LatestImage"] != null) //{ // ViewBag.LatestImage = Convert.ToString(TempData["LatestImage"]); //} return(View(model)); }