Exemple #1
0
 /// <summary>
 /// 生成缩略图
 /// </summary>
 /// <param name="originalPath">源图路径</param>
 /// <param name="thumbnailPath">生成后缩略图路径</param>
 /// <param name="toWidth">目标宽度</param>
 /// <param name="toHeight">目标高度</param>
 /// <param name="thumbsType">生成类型</param>
 /// <returns></returns>
 public static bool ThumbsImage(string originalPath, string thumbnailPath, int toWidth, int toHeight, ThumbsType thumbsType)
 {
     return(ThumbsImage(GetBitmap(originalPath), thumbnailPath, toWidth, toHeight, thumbsType));
 }
Exemple #2
0
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImage">源图</param>
        /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
        /// <param name="toWidth">最终缩略图宽度</param>
        /// <param name="toHeight">最终缩略图高度</param>
        /// <param name="thumbsType">生成缩略图的方式</param>
        public static bool ThumbsImage(Image originalImage, string thumbnailPath, int toWidth, int toHeight, ThumbsType thumbsType)
        {
            int num  = toWidth;
            int num2 = toHeight;
            int num3 = toWidth;
            int num4 = toHeight;
            int x    = 0;
            int y    = 0;
            int num5 = originalImage.Width;
            int num6 = originalImage.Height;
            int x2   = 0;
            int y2   = 0;

            switch (thumbsType)
            {
            case ThumbsType.Fill:
                num4 = toHeight;
                num3 = num4 * num5 / num6;
                if (num3 > toWidth)
                {
                    num4 = num4 * toWidth / num3;
                    num3 = toWidth;
                }
                x2 = (toWidth - num3) / 2;
                y2 = (toHeight - num4) / 2;
                break;

            case ThumbsType.HeightAndWidth:
                num4 = (num2 = originalImage.Height * toWidth / originalImage.Width);
                num3 = (num = originalImage.Width * toHeight / originalImage.Height);
                break;

            case ThumbsType.Width:
                num4 = (num2 = originalImage.Height * toWidth / originalImage.Width);
                break;

            case ThumbsType.Height:
                num3 = (num = originalImage.Width * toHeight / originalImage.Height);
                break;

            case ThumbsType.Cut:
                if ((double)originalImage.Width / (double)originalImage.Height > (double)num / (double)num2)
                {
                    num6 = originalImage.Height;
                    num5 = originalImage.Height * num / num2;
                    y    = 0;
                    x    = (originalImage.Width - num5) / 2;
                }
                else
                {
                    num5 = originalImage.Width;
                    num6 = originalImage.Width * toHeight / num;
                    x    = 0;
                    y    = (originalImage.Height - num6) / 2;
                }
                break;
            }
            Bitmap   bitmap   = new Bitmap(num, num2);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.InterpolationMode  = InterpolationMode.High;
            graphics.SmoothingMode      = SmoothingMode.HighQuality;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode  = InterpolationMode.High;
            if (thumbnailPath.EndsWith(".png", true, CultureInfo.CurrentCulture))
            {
                graphics.Clear(Color.Transparent);
            }
            else
            {
                graphics.Clear(Color.White);
            }
            graphics.Clear(Color.White);
            graphics.DrawImage(originalImage, new Rectangle(x2, y2, num3, num4), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
            try
            {
                return(CompressImage(bitmap, thumbnailPath));
            }
            catch
            {
                return(false);
            }
            finally
            {
                bitmap.Dispose();
                graphics.Dispose();
            }
        }
Exemple #3
0
 /// <summary>
 /// 缩小图片
 /// </summary>
 /// <param name="filePath">文件路径</param>
 /// <param name="width">缩略图宽</param>
 /// <param name="height">缩略图高</param>
 /// <param name="type">缩放方式</param>
 /// <returns></returns>
 public static bool Resize(string filePath, int width, int height, ThumbsType type)
 {
     return(ThumbsImage(GetBitmap(filePath), FileHelper.GetTruePath(filePath), width, height, type));
 }