/// <summary> /// Initializes a new instance of the <see cref="FormattedImage"/> class. /// </summary> /// <param name="image">The image.</param> /// <param name="format">The format.</param> /// <param name="keepOpen">Whether to keep the source image open upon disposing the <see cref="FormattedImage"/> object.</param> private FormattedImage(Image image, IImageFormat format, bool keepOpen) { this.Image = image; this.imageFormatsManager = image.GetConfiguration().ImageFormatsManager; this.Format = format; this.keepOpen = keepOpen; }
/// <inheritdoc /> public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context) { ImageFormatManager formatManager = new ImageFormatManager(); return(inputs.SelectMany(context, input => { FilePath relativePath = input.FilePath(Keys.RelativeFilePath); return _operations .SelectMany(operations => { FilePath destinationPath = relativePath == null ? null : context.FileSystem.GetOutputPath(relativePath); // Get the image Image <Rgba32> image; IImageFormat imageFormat; using (Stream stream = input.GetStream()) { image = SixLabors.ImageSharp.Image.Load(stream, out imageFormat); } // Mutate the image with the specified operations, if there are any if (operations.Operations.Count > 0) { image.Mutate(imageContext => { IImageProcessingContext <Rgba32> workingImageContext = imageContext; foreach (IImageOperation operation in operations.Operations) { // Apply operation workingImageContext = operation.Apply(workingImageContext); // Modify the path if (destinationPath != null) { destinationPath = operation.GetPath(destinationPath) ?? destinationPath; } } }); } // Invoke output actions IEnumerable <OutputAction> outputActions = operations.OutputActions.Count == 0 ? (IEnumerable <OutputAction>) new[] { new OutputAction((i, s) => i.Save(s, imageFormat), null) } : operations.OutputActions; return outputActions.Select(action => { FilePath formatPath = action.GetPath(destinationPath) ?? destinationPath; Trace.Verbose($"{Keys.WritePath}: {formatPath}"); MemoryStream outputStream = new MemoryStream(); action.Invoke(image, outputStream); outputStream.Seek(0, SeekOrigin.Begin); return context.GetDocument(input, outputStream, new MetadataItems { { Keys.WritePath, formatPath } }); }); }); })); }
private static ImageFormatManager GetImageFormatManager() { var manager = new ImageFormatManager(); manager.AddImageFormat(JpegFormat.Instance); manager.AddImageFormat(PngFormat.Instance); return(manager); }
public GravatarProvider( IHttpClientFactory httpClientFactory, ILogger <IAvatarProvider> logger, ImageFormatManager imageFormatManager, IImageResizer imageResizer) { _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _imageFormatManager = imageFormatManager ?? throw new ArgumentNullException(nameof(imageFormatManager)); _imageResizer = imageResizer ?? throw new ArgumentNullException(nameof(imageResizer)); }
public AzureDevOpsAvatarProvider( IAzureDevOpsClient client, IUserRepository userRepository, ILogger <IAvatarProvider> logger, ImageFormatManager imageFormatManager, IImageResizer imageResizer) { _client = client; _userRepository = userRepository ?? throw new ArgumentNullException(nameof(userRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _imageFormatManager = imageFormatManager ?? throw new ArgumentNullException(nameof(imageFormatManager)); _imageResizer = imageResizer ?? throw new ArgumentNullException(nameof(imageResizer)); }
public static string UploadImage(IFormFile file, string path, string fileName, int width, int height, bool maintainAspectRatio = false) { string fileExtension = Path.GetExtension(file.FileName); string fileNameWithExtension = fileName + fileExtension; string pathWithFileName = Path.Combine(path, fileNameWithExtension); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string[] files = Directory.GetFiles(path, fileName + ".*"); foreach (string item in files) { File.Delete(item); } if (file.Length > 0) { using (var stream = new FileStream(pathWithFileName, FileMode.Create)) { using (Image <Rgba32> image = Image.Load(file.OpenReadStream())) { int destinationWidth = width; int destinationHeight = height; int originalWidth = image.Width; int originalHeight = image.Height; if (maintainAspectRatio && destinationWidth < originalWidth) { double ratio = 0; ratio = (double)destinationWidth / (double)originalWidth; destinationWidth = Convert.ToInt32(originalWidth * ratio); destinationHeight = Convert.ToInt32(originalHeight * ratio); } image.Mutate(x => x.Resize(destinationWidth, destinationHeight)); ImageFormatManager imageFormatManager = new ImageFormatManager(); imageFormatManager.AddImageFormat(ImageFormats.Jpeg); imageFormatManager.AddImageFormat(ImageFormats.Png); image.Save(stream, imageFormatManager.FindFormatByFileExtension(fileExtension)); } } } return(fileNameWithExtension); }
public AzureDevOpsAvatarProvider( IUserRepository userRepository, ILogger <IAvatarProvider> logger, IAzureDevOpsConfig config, IHttpClientFactory httpClientFactory, ImageFormatManager imageFormatManager, IImageResizer imageResizer) { _userRepository = userRepository ?? throw new ArgumentNullException(nameof(userRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _config = config ?? throw new ArgumentNullException(nameof(config)); _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); _imageFormatManager = imageFormatManager ?? throw new ArgumentNullException(nameof(imageFormatManager)); _imageResizer = imageResizer ?? throw new ArgumentNullException(nameof(imageResizer)); }
private IImageEncoder ConvertFormat(TextureImageFormat format) { ImageFormatManager imageFormatManager = new ImageFormatManager(); IImageFormat imageFormat = null; if (format == TextureImageFormat.image_jpeg) { imageFormat = imageFormatManager.FindFormatByFileExtension(".jpg"); } else { imageFormat = imageFormatManager.FindFormatByFileExtension(".png"); } return(imageFormatManager.FindEncoder(imageFormat)); }
/// <summary> /// Initializes a new instance of the <see cref="FormattedImage"/> class. /// </summary> /// <param name="image">The image.</param> /// <param name="format">The format.</param> internal FormattedImage(Image <Rgba32> image, IImageFormat format) { this.Image = image; this.imageFormatsManager = image.GetConfiguration().ImageFormatsManager; this.Format = format; }
public ImageFormatManagerTests() { this.DefaultFormatsManager = Configuration.CreateDefaultInstance().ImageFormatsManager; this.FormatsManagerEmpty = new ImageFormatManager(); }
public ImageFormatManagerTests() { this.DefaultFormatsManager = Configuration.Default.ImageFormatsManager; this.FormatsManagerEmpty = new ImageFormatManager(); }