Menu

asp.net core / C# Image Resize Service using ImageMagick NuGet

Install the ImageMagick nuGet, below is the service implementation.

Please make sure to remove the “data:image/jpeg;base64,” or “data:image/png;base64,” part from the the base 64 image string you pass to the method.

    public class ImageResizeService : IImageResizeService
    {
        public byte[] ResizeIfLargerThan(string data, int maxWidth, int maxHeight)
        {
            var bytes = Convert.FromBase64String(data);
            var image = new MagickImage(bytes);

            var size = new MagickGeometry(maxWidth, maxHeight);
            image.Resize(size);
            return image.ToByteArray();
        }
    }

 

Leave a comment