public async Task <BaseImage> GenerateGrayAsync() { if (!_sourceImage.IsRgb) { throw new Exception($"The image {_sourceImage.Id} is already converted to gray. Use the original one"); } using (Stream sourceStream = File.OpenRead(_sourceImage.GetFileServerPath())) using (var sourceImg = Image.Load(sourceStream)) { sourceImg.Mutate(x => x.Grayscale()); return(await BaseImage.CreateAsync(sourceImg, _sourceImage.Name, Effect.ConvertToGrayEffect(_sourceImage.AppliedEffects.LastOrDefault()), _sourceImage, _sourceImage.FacePoints, xTop : _sourceImage.XTopLeftOriginal, yTop : _sourceImage.YTopLeftOriginal, xBottom : _sourceImage.XBottomRightOriginal, yBottom : _sourceImage.YBottomRightOriginal, isRgb : false).ConfigureAwait(false)); } }
public static async Task <BaseImage> CreateAsync <TColor>(Image <TColor> sourceImage, string name, Effect newEffect = null, BaseImage parentImage = null, IEnumerable <Vector2> facePoints = null, bool isRgb = true, int?xTop = null, int?yTop = null, int?xBottom = null, int?yBottom = null) where TColor : struct, IPixel <TColor> { BaseImage img = new BaseImage { Name = name, Width = sourceImage.Width, Height = sourceImage.Height, }; SetImageCommonProperties(img, newEffect, parentImage, facePoints, isRgb, (xTop, yTop, xBottom, yBottom)); var fsPath = img.GetFileServerPath(); var fInfoDest = new FileInfo(fsPath); if (!fInfoDest.Directory.Exists) { fInfoDest.Directory.Create(); } using (var writeStream = File.Create(fsPath)) { sourceImage.SaveAsJpeg(writeStream, new JpegEncoder { Quality = 100 }); } return(await Task.FromResult(img).ConfigureAwait(false)); }
public static async Task <BaseImage> CreateAsync(string fileName, Effect newEffect = null, BaseImage parentImage = null, bool copyToFileServer = true, IEnumerable <Vector2> facePoints = null, bool isRgb = true, int?xTop = null, int?yTop = null, int?xBottom = null, int?yBottom = null) { var fInfo = new FileInfo(fileName); BaseImage img = new BaseImage { Name = fInfo.Name, }; img.GetDimensions(fileName); SetImageCommonProperties(img, newEffect, parentImage, facePoints, isRgb, (xTop, yTop, xBottom, yBottom)); if (copyToFileServer) { var fsPath = img.GetFileServerPath(); var fInfoDest = new FileInfo(fsPath); if (!fInfoDest.Directory.Exists) { fInfoDest.Directory.Create(); } using (var readStream = File.OpenRead(fileName)) using (var writeStream = File.Create(fsPath)) { await readStream.CopyToAsync(writeStream).ConfigureAwait(false); } } return(img); }