예제 #1
0
 /// <summary>
 /// 保存缩略图到磁盘(可指定宽高,可指定缩放模式)
 /// </summary>
 /// <param name="srcPath"></param>
 /// <param name="destPath"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="mode"></param>
 public static void SaveThumbnail(String srcPath, String destPath, int width, int height)
 {
     using (Image srcImg = Image.FromFile(srcPath))
     {
         ThumbSize t = getCutSize(width, height, srcImg);
         using (Bitmap newImg = new Bitmap(t.New.Width, t.New.Height))
         {
             using (Graphics g = Graphics.FromImage(newImg))
             {
                 g.InterpolationMode = InterpolationMode.High;
                 g.SmoothingMode     = SmoothingMode.HighQuality;
                 g.Clear(Color.Transparent);
                 g.DrawImage(srcImg, t.getNewRect(), t.getRect(), GraphicsUnit.Pixel);
                 try
                 {
                     newImg.Save(destPath, ImageFormat.Jpeg);
                 }
                 catch (Exception e)
                 {
                     throw e;
                 }
             }
         }
     }
 }
예제 #2
0
        private static ThumbSize getCutSize(int width, int height, Image src)
        {
            ThumbSize t = new ThumbSize();

            if ((double)src.Width / (double)src.Height > (double)width / (double)height)
            {
                int newWidth = src.Height * width / height;
                t.Src   = new Size(newWidth, src.Height);
                t.New   = new Size(width, height);
                t.Point = new Point((src.Width - newWidth) / 2, 0);
            }
            else
            {
                int newHeight = src.Width * height / width;
                t.Src   = new Size(src.Width, newHeight);
                t.New   = new Size(width, height);
                t.Point = new Point(0, (src.Height - newHeight) / 2);
            }
            return(t);
        }