Class that can be used to optimize an image.
Inheritance: ILosslessImageOptimizer
    public void Test_IsSupported()
    {
      ImageOptimizer optimizer = new ImageOptimizer();

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        optimizer.IsSupported((FileInfo)null);
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        optimizer.IsSupported((string)null);
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        optimizer.IsSupported("");
      });

      Assert.IsTrue(optimizer.IsSupported(Files.FujiFilmFinePixS1ProGIF));
      Assert.IsTrue(optimizer.IsSupported(Files.ImageMagickJPG));
      Assert.IsTrue(optimizer.IsSupported(Files.SnakewarePNG));
      Assert.IsTrue(optimizer.IsSupported(Files.Missing));
      Assert.IsFalse(optimizer.IsSupported(Files.InvitationTif));
    }
    public static void MakeGooglePageSpeedInsightsHappy()
    {
      FileInfo snakewareLogo = new FileInfo(SampleFiles.OutputDirectory + "OptimizeTest.jpg");
      File.Copy(SampleFiles.SnakewareJpg, snakewareLogo.FullName, true);

      Console.WriteLine("Bytes before: " + snakewareLogo.Length);

      ImageOptimizer optimizer = new ImageOptimizer();
      optimizer.LosslessCompress(snakewareLogo);

      snakewareLogo.Refresh();
      Console.WriteLine("Bytes after:  " + snakewareLogo.Length);
    }
Example #3
0
        public async Task <string> OptimizeImage(IFormFile inputImage, int endHeight, int endWidth)
        {
            double aspectRatioCoeff = endHeight / endWidth;
            var    imageName        = Guid.NewGuid() + ".jpg";
            var    inputFileName    = Path.Combine(hostingEnvironment.WebRootPath, "images/") + imageName;

            using (var initialImageStream = new MemoryStream())
            {
                inputImage.CopyTo(initialImageStream);

                //Compress
                initialImageStream.Position = 0;
                var optimizer = new ImageMagick.ImageOptimizer();
                optimizer.LosslessCompress(initialImageStream);
                initialImageStream.Position = 0;

                //Resize
                using (MagickImage image = new MagickImage(initialImageStream))
                {
                    double tempWidth  = 0;
                    double tempHeight = 0;

                    double imgHeight   = image.Height;
                    double imgWidth    = image.Width;
                    double aspectRatio = imgHeight / imgWidth;

                    var scale = Math.Max(endWidth / imgWidth, endHeight / imgHeight);

                    tempWidth  = imgWidth * scale;
                    tempHeight = imgHeight * scale;

                    int resultedSize = (int)Math.Max(tempWidth, tempHeight);

                    MagickGeometry size = new MagickGeometry(resultedSize)
                    {
                        IgnoreAspectRatio = false
                    };

                    image.Resize(size);
                    var resizedImageByteArr = image.ToByteArray();

                    using (var finalImageStream = new MemoryStream(resizedImageByteArr))
                    {
                        //Crop
                        var cropRect = new Rectangle(0, 0, endWidth, endHeight);
                        using (Bitmap src = new Bitmap(finalImageStream))
                        {
                            using (Bitmap target = new Bitmap(cropRect.Width, cropRect.Height))
                            {
                                var      result = new Rectangle(0, 0, target.Width, target.Height);
                                Graphics g      = Graphics.FromImage(target);
                                g.DrawImage(src, result, cropRect, GraphicsUnit.Pixel);

                                target.Save(inputFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }
                    }
                }
            }
            return(imageName);
        }