public Color DetectDominantColour(Bitmap image) { var calculator = new DominantHueColorCalculator(); var color = calculator.CalculateDominantColor(image); return(color); }
private static async Task <GalleryFolder> ProcessMediaInternalAsync(ILogger log, string name, string folder, string description) { log.LogInformation("C# HTTP process media trigger function processed a request."); var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage")); var blobClient = storageAccount.CreateCloudBlobClient(); var srcContainer = blobClient.GetContainerReference("image-upload"); var container = blobClient.GetContainerReference("images"); JsonSerializerOptions options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; GalleryFolder root = new GalleryFolder() { Id = Guid.Empty.ToString() }; // load images.json var blob = container.GetBlockBlobReference(imagesJsonFilename); if (blob != null) { if (await blob.ExistsAsync()) { var body = await blob.DownloadTextAsync(); if (!string.IsNullOrWhiteSpace(body)) { root = JsonSerializer.Deserialize <GalleryFolder>(body, options); } } // load file var f = srcContainer.GetBlockBlobReference(name); using MemoryStream stream = new MemoryStream(); await f.DownloadToStreamAsync(stream).ConfigureAwait(true); stream.Position = 0; using var src = Image.Load(stream); var id = Guid.NewGuid(); var hueCalc = new DominantHueColorCalculator(0.5f, 0.5f, 60); var galleryImage = new GalleryImage() { Id = id, DominantColour = "#" + hueCalc.CalculateDominantColor(src).ToHex(), Files = new Dictionary <string, GalleryImageDetails>(), Description = description }; var resolutions = GetResolutions(); foreach (var key in resolutions.Keys) { galleryImage.Files[key] = await CreatePreviewImageAsync( src.Clone(x => x.AutoOrient()), key, container, id, resolutions ); } if (!string.IsNullOrWhiteSpace(folder)) { (GetFolderRecursive(root, folder) ?? root) .Images.Add(galleryImage); } else { root.Images.Add(galleryImage); } // save images.json var outputBody = JsonSerializer.Serialize(root, options); await blob.UploadTextAsync(outputBody); log.LogInformation("Updated images.json"); // delete original file await f.DeleteAsync().ConfigureAwait(true); } return(root); }