예제 #1
0
 /// <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;
 }
예제 #2
0
        /// <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 }
                        });
                    });
                });
            }));
        }
예제 #3
0
        private static ImageFormatManager GetImageFormatManager()
        {
            var manager = new ImageFormatManager();

            manager.AddImageFormat(JpegFormat.Instance);
            manager.AddImageFormat(PngFormat.Instance);

            return(manager);
        }
예제 #4
0
 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));
 }
예제 #5
0
 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));
 }
예제 #6
0
        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);
        }
예제 #7
0
 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));
 }
예제 #8
0
        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));
        }
예제 #9
0
 /// <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;
 }
예제 #10
0
 public ImageFormatManagerTests()
 {
     this.DefaultFormatsManager = Configuration.CreateDefaultInstance().ImageFormatsManager;
     this.FormatsManagerEmpty   = new ImageFormatManager();
 }
예제 #11
0
 public ImageFormatManagerTests()
 {
     this.DefaultFormatsManager = Configuration.Default.ImageFormatsManager;
     this.FormatsManagerEmpty   = new ImageFormatManager();
 }