图片压缩

实体:

   public class ThumbSize
    {
        public string SizeName { get; set; }

        public int Quality { get; set; }

        public int Size { get; set; }

    }

配置文件:

<?xml version="1.0"?>
<ArrayOfThumbSize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ThumbSize>
    <SizeName>Lagre</SizeName>
    <Quality>90</Quality>
    <Size>2048</Size>
  </ThumbSize>
  <ThumbSize>
    <SizeName>Middle</SizeName>
    <Quality>100</Quality>
    <Size>512</Size>
  </ThumbSize>
  <ThumbSize>
    <SizeName>Small</SizeName>
    <Quality>100</Quality>
    <Size>256</Size>
  </ThumbSize>
</ArrayOfThumbSize>

图片压缩方法:

 public static bool GetPicThumbnail(string sFile, List<ThumbSize> sizeList)
        {
            string dFile = string.Empty;
            int dHeight = 64, dWidth = 64;//宽 高
            var extension = Path.GetExtension(sFile);//后缀
            Image iSource = Image.FromFile(sFile);
            var rate = iSource.Width * 1.00 / iSource.Height;
            var temp = sFile.Replace(extension, "");
         
            foreach (var item in sizeList)
            {
                dFile = temp + "-"+item.SizeName + extension;//生成的压缩文件名 
                if (File.Exists(dFile))
                {
                    File.Delete(dFile);
                }
                if (rate <= 1)
                {
                    dHeight = item.Size;
                    dWidth = (int)(rate * item.Size);
                }
                else
                {
                    dWidth = item.Size;
                    dHeight = (int)(item.Size / rate);
                }

                ImageFormat tFormat = iSource.RawFormat;
                int sW = dWidth, sH = dHeight;
                Bitmap ob = new Bitmap(dWidth, dHeight);
                Graphics g = Graphics.FromImage(ob);
                g.Clear(Color.WhiteSmoke);
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
                g.Dispose();
                //以下代码为保存图片时,设置压缩质量           
                EncoderParameters ep = new EncoderParameters();
                long[] qy = new long[1];
                qy[0] = item.Quality;//设置压缩的比例1-100
                EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
                ep.Param[0] = eParam;

                try
                {
                    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo jpegICIinfo = null;
                    for (int x = 0; x < arrayICI.Length; x++)
                    {
                        if (arrayICI[x].FormatDescription.Equals("JPEG"))
                        {
                            jpegICIinfo = arrayICI[x];
                            break;
                        }
                    }
                    if (jpegICIinfo != null)
                    {
                        ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
                        Console.WriteLine("生成压缩文件:"+dFile);
                    }
                    else
                    {
                        ob.Save(dFile, tFormat);
                    }                  
                }
                catch
                {
                    return false;
                }
                finally
                {                   
                    ob.Dispose();
                }
            }
            iSource.Dispose();
            return true;
        }

调用:

            var path = Environment.CurrentDirectory;
            Console.WriteLine(path);
            var files = Directory.GetFiles(path);
            foreach (var item in files.Where(o => 
            (o.EndsWith(".tiff", StringComparison.InvariantCultureIgnoreCase) ||
            o.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) ||
            o.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) ||
            o.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) ||
            o.EndsWith(".bmp", StringComparison.InvariantCultureIgnoreCase))))
            {
                bool falg = true;
                string fileName = Path.GetFileName(item);
                foreach (var sizeName in sizeList.Select(s=>s.SizeName))
                {
                    if (fileName.IndexOf(sizeName, StringComparison.InvariantCultureIgnoreCase) > 0)
                    {
                        falg = false;
                        break;
                    }
                       
                }
                if (falg)
                {
                    GetPicThumbnail(item, sizeList);
                }
               
            }

 

posted @ 2017-04-28 10:03  我没有领悟  阅读(183)  评论(0编辑  收藏  举报