public void ShouldThrowExceptionWhenPointDColorIsNull() { Assert.Throws <ArgumentNullException>("color", () => { using (var image = new MagickImage(MagickColors.White, 2, 2)) { image.FloodFill((MagickColor)null, default); }
public void ShouldThrowExceptionWhenColorIsNull() { ExceptionAssert.ThrowsArgumentNullException("color", () => { using (IMagickImage image = new MagickImage(MagickColors.White, 2, 2)) { image.FloodFill((MagickColor)null, 0, 0); } }); }
public static async Task ExtractAlpha(string inputDir, string outputDir, bool print = true, bool setProgress = true, bool removeInputAlpha = true) { try { var files = IOUtils.GetFilesSorted(inputDir); if (print) { Logger.Log($"Extracting alpha channel from images..."); } Directory.CreateDirectory(outputDir); Stopwatch sw = new Stopwatch(); sw.Restart(); int counter = 0; foreach (string file in files) { MagickImage alphaImg = new MagickImage(file); if (removeInputAlpha) { MagickImage rgbImg = alphaImg; rgbImg.Format = MagickFormat.Png24; rgbImg.Quality = 10; MagickImage bg = new MagickImage(MagickColors.Black, rgbImg.Width, rgbImg.Height); bg.Composite(rgbImg, CompositeOperator.Over); rgbImg = bg; rgbImg.Write(file); } alphaImg.Format = MagickFormat.Png24; alphaImg.Quality = 10; alphaImg.FloodFill(MagickColors.None, 0, 0); // Fill the image with a transparent background alphaImg.InverseOpaque(MagickColors.None, MagickColors.White); // Change all the pixels that are not transparent to white. alphaImg.ColorAlpha(MagickColors.Black); // Change the transparent pixels to black. string outPath = Path.Combine(outputDir, Path.GetFileName(file)); alphaImg.Write(outPath); counter++; if (sw.ElapsedMilliseconds > 250) { if (setProgress) { Program.mainForm.SetProgress((int)Math.Round(((float)counter / files.Length) * 100f)); } await Task.Delay(1); sw.Restart(); } } } catch (Exception e) { Logger.Log("ExtractAlpha Error: " + e.Message); } }
/// <summary> /// Method to perform file modification using Magick.Net /// </summary> /// <returns>Path to the new file ( modified )</returns> public async Task <DefaultServiceResponse> ModifyFile(ModifyModel model) { // Get last uploaded photo by the customer var lastUploadedImageName = DataContext.CustomerImageFiles.ToList().Where(imageFile => imageFile.CustomerId == model.UserId).OrderByDescending(orderBy => orderBy.UploadTime).Select(response => response.FullName); var intesivity = model.Intesivity; var editedFileName = ""; using (var image = new MagickImage(lastUploadedImageName.First())) { switch (model.SelectedOperation) { case "CycleColorMap": image.CycleColormap(intesivity); break; case "FloodFill": image.FloodFill(10, 10, 10); break; case "Flop": image.Flop(); break; case "GammaCorrect": image.GammaCorrect(intesivity); // gramma break; case "GausiianBlur": image.GaussianBlur(intesivity, intesivity); // double radius + double sigma break; case "MedianFilter": image.MedianFilter(intesivity); // radius break; case "MotionBlur": image.MotionBlur(10, 10, 10); break; case "Negate": image.Negate(); // simple negate break; default: break; } if (model.UseFrame) { image.Border(7, 7); } editedFileName = lastUploadedImageName.First().Replace(Path.GetFileNameWithoutExtension(lastUploadedImageName.First()), Path.GetFileNameWithoutExtension(lastUploadedImageName.First()) + "_modified").Replace(Path.GetExtension(lastUploadedImageName.First()), "." + model.OutputFileType.ToLower()); using (File.Create(editedFileName)) image.Write(editedFileName); } // Save file to the DB var originalImageId = DataContext.CustomerImageFiles.Where(imageData => imageData.CustomerId == model.UserId && imageData.FullName.Contains(Path.GetFileNameWithoutExtension(lastUploadedImageName.First()))).Select(result => result.Id).First(); await DataContext.CustomerEditedImageFiles.AddAsync(new ResultImageFIleModel { Id = new Guid(), CustomerId = model.UserId, FullName = editedFileName, OriginalImageId = originalImageId.ToString(), UploadTime = DateTime.Now }); await DataContext.SaveChangesAsync(); return(new DefaultServiceResponse { ResponseData = "../" + Path.Combine("CustomersImages", Path.GetFileName(editedFileName)) }); }