asp.net生成缩略图

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.IO;
namespace ImageBll
{
    public class ImageUtility
    {
        /// <summary>
        /// 生成缩略图的方法
        /// </summary>
        /// <param name="Path">原始图片路径</param>
        /// <param name="StPath">生成图片存放的路径</param>
        /// <param name="width">生成图片的宽度</param>
        /// <param name="height">生成图片的高度</param>
        /// <param name="mode">生成的模式( "HW":指定高宽缩放;"W"://指定宽度,计算缩略图;"H":指定高度,计算缩略图;"CUT":)</param>
        /// <returns></returns>
        public static  string  MakeThumbImage(string Path, string StPath, int width, int height, string mode)
        {
            string messages = string.Empty;
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            int tw = width;
            int th = height;
            //原始图片的宽度和高度
            int sw = image.Width;
            int sh = image.Height;
            int x = 0, y = 0;
            switch (mode)
            {
                case "HW"://指定高宽缩放
                    break;
                case"W"://按比例缩放,指定宽度,计算缩略图的高度
                    th = image.Height * width / image.Width;
                    break;
                case "H"://按比例缩放,指定高度,计算缩略图的高度
                    tw = image.Width * height / image.Height;
                    break;
                case "CUT":
                    if ((double)tw / (double)th < (double)width / (double)height)
                    {
                        sw = image.Width;
                        sh = image.Width * height / tw;
                        x = 0;
                        y = (image.Height - sh) / 2;
                    }
                    else
                    {
                        sh = image.Height;
                        sw = image.Height * width / th;
                        y = 0;
                        x = (image.Width - sw) / 2;
                    }
                    break;
                default: break;
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(System.Drawing.Color.Transparent);
            g.DrawImage(image,
                new System.Drawing.Rectangle(x, y, tw, th),
                new System.Drawing.Rectangle(x, y, sw, sh), 
                System.Drawing.GraphicsUnit.Pixel);
            try
            {
                bitmap.Save(StPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                messages= ex.Message;
            }
            finally
            {
                image.Dispose(); 
                bitmap.Dispose();
                g.Dispose();
                if (messages == string.Empty)
                {
                    messages = "成功生成!";
                }
                
            }

            return messages;
        }
    }
}
posted @ 2009-03-13 20:23  下里巴人or知己  阅读(468)  评论(0编辑  收藏  举报