Beispiel #1
0
 private void Resize(string fullPath, ResizeRule rule)
 {
     if (rule.Width.HasValue && !rule.Height.HasValue)
     {
         var binary     = BinaryHelper.FromImageToString(fullPath);
         var outputPath = Path.Combine(rule.OutputPath, Path.GetFileName(fullPath));
         ImageHelper.ResizeImageW(binary, rule.Width.Value, outputPath);
     }
     else if (rule.Height.HasValue && !rule.Width.HasValue)
     {
         var binary     = BinaryHelper.FromImageToString(fullPath);
         var outputPath = Path.Combine(rule.OutputPath, Path.GetFileName(fullPath));
         ImageHelper.ResizeImageH(binary, rule.Height.Value, outputPath);
     }
     else
     {
         var binary     = BinaryHelper.FromImageToString(fullPath);
         var outputPath = Path.Combine(rule.OutputPath, Path.GetFileName(fullPath));
         ImageHelper.ResizeImage(binary, rule.Width.Value, rule.Height.Value, outputPath);
     }
 }
        static public string ResizeImage(string image, int maxWidth, int maxHeight, string destinationPath)
        {
            byte[] rebin        = Convert.FromBase64String(image);
            var    tempFileName = Path.Combine(Path.GetDirectoryName(destinationPath), $"{Guid.NewGuid()}.jpg");

            using (FileStream fs2 = new FileStream(tempFileName, FileMode.Create))
                using (BinaryWriter bw = new BinaryWriter(fs2))
                    bw.Write(rebin);
            using (var img = Bitmap.FromFile(tempFileName))
            {
                using (var bitmap = new Bitmap(img))
                {
                    ResizeImage(bitmap, maxWidth, maxHeight, 100, destinationPath);
                }
            }
            if (File.Exists(tempFileName))
            {
                File.Delete(tempFileName);
            }
            return(BinaryHelper.FromImageToString(destinationPath));
        }