当前位置: 代码迷 >> ASP.NET >> 上传缩略图怎么实现不变形?等比例缩放
  详细解决方案

上传缩略图怎么实现不变形?等比例缩放

热度:4100   发布时间:2013-02-25 00:00:00.0
上传缩略图如何实现不变形?等比例缩放
目前上传图片和生成缩略图的代码:
C# code
//这是标题图片上传方法                if (image != null)                {                    string year = DateTime.Now.Year.ToString();                    string month = DateTime.Now.Month.ToString("00");                    string day = DateTime.Now.Day.ToString("00");                    string fileFolder = string.Concat("Upload/", year, "/", month, "/", day);                    string path = HostingEnvironment.MapPath("~/" + fileFolder);                    if (!Directory.Exists(path))                    {                        //在Upload目录下创建了一个文件夹                           Directory.CreateDirectory(path);                    }                    string imagename = image.FileName;                    string fileType = Path.GetExtension(image.FileName).ToLower();                    if (fileType == ".jpeg" || fileType == ".jpg" || fileType == ".png" || fileType == ".gif")                    {                        Random ran = new Random();                        string ename = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ran.Next(9999);                        //上传后生成的图片名                        string name = ename + fileType;                        image.SaveAs(Path.Combine(path, name));                        string imageurl = string.Concat(HostingEnvironment.ApplicationVirtualPath, fileFolder, "/", name);                        //生成标题图片                        //缩略图名                        string thumbName = ename + "_500x200" + fileType;                        //缩略图绝对路径                        string thumbPath = string.Concat(HostingEnvironment.ApplicationPhysicalPath, fileFolder, "/", thumbName);                        // 设置缩略图保存路径                        FileStream stream = new FileStream(Path.GetFullPath(thumbPath), FileMode.OpenOrCreate);                        // 缩放上传的文件                        Image OrigImage = Image.FromStream(image.InputStream);                        // 创建缩略图对象                        Bitmap TempBitmap = new Bitmap(550, 200);                        // 创建缩略图画质                        Graphics NewImage = Graphics.FromImage(TempBitmap);                        NewImage.CompositingQuality = CompositingQuality.HighQuality;                        NewImage.SmoothingMode = SmoothingMode.HighQuality;                        NewImage.InterpolationMode = InterpolationMode.HighQualityBicubic;                        // 创建Rectangle对象进行绘制                        Rectangle imageRectangle = new Rectangle(0, 0, 550, 200);                        NewImage.DrawImage(OrigImage, imageRectangle);                        // 保存缩略图                        TempBitmap.Save(stream, OrigImage.RawFormat);                        // 释放资源                        NewImage.Dispose();                        TempBitmap.Dispose();                        OrigImage.Dispose();                        stream.Close();                        stream.Dispose();                        newArticle.ImageUrl = imageurl;                    }                }


但是这样,直接指定了宽、高,难免会造成图片变形。所以想改造在成宽度固定550,然后高度自适应。相当于windows中的图片查看器,改变窗体大小,图片也会相应的改变,图片不变形。
这个应该就是等比例缩话吧,上面的代码如何改造 ?

------解决方案--------------------------------------------------------
Asp.net真正生成高质量不变形缩略图片
http://www.cnblogs.com/leeolevis/archive/2009/02/03/1383132
------解决方案--------------------------------------------------------

C# code
/// <summary>/// 获取一个图片按等比例缩小后的大小。/// </summary>/// <param name="maxWidth">需要缩小到的宽度</param>/// <param name="maxHeight">需要缩小到的高度</param>/// <param name="imageOriginalWidth">图片的原始宽度</param>/// <param name="imageOriginalHeight">图片的原始高度</param>/// <returns>返回图片按等比例缩小后的实际大小</returns>public static Size GetNewSize(int maxWidth, int maxHeight, int imageOriginalWidth, int imageOriginalHeight){    double w = 0.0;    double h = 0.0;    double sw = Convert.ToDouble(imageOriginalWidth);    double sh = Convert.ToDouble(imageOriginalHeight);    double mw = Convert.ToDouble(maxWidth);    double mh = Convert.ToDouble(maxHeight);    if (sw < mw && sh < mh)    {        w = sw;        h = sh;    }    else if ((sw / sh) > (mw / mh))    {        w = maxWidth;        h = (w * sh) / sw;    }    else    {        h = maxHeight;        w = (h * sw) / sh;    }    return new Size(Convert.ToInt32(w), Convert.ToInt32(h));}/// <summary>/// 对给定的一个图片(Image对象)生成一个指定大小的缩略图。/// </summary>/// <param name="originalImage">原始图片</param>/// <param name="thumMaxWidth">缩略图的宽度</param>/// <param name="thumMaxHeight">缩略图的高度</param>/// <returns>返回缩略图的Image对象</returns>public static System.Drawing.Image GetThumbNailImage(System.Drawing.Image originalImage, int thumMaxWidth, int thumMaxHeight){    Size thumRealSize = Size.Empty;    System.Drawing.Image newImage = originalImage;    Graphics graphics = null;    try    {        thumRealSize = GetNewSize(thumMaxWidth, thumMaxHeight, originalImage.Width, originalImage.Height);        newImage = new Bitmap(thumRealSize.Width, thumRealSize.Height);        graphics = Graphics.FromImage(newImage);        graphics.CompositingQuality = CompositingQuality.HighQuality;        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;        graphics.SmoothingMode = SmoothingMode.HighQuality;        graphics.Clear(Color.Transparent);        graphics.DrawImage(originalImage, new Rectangle(0, 0, thumRealSize.Width, thumRealSize.Height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel);    }    catch { }    finally    {        if (graphics != null)        {            graphics.Dispose();            graphics = null;        }    }    return newImage;}
  相关解决方案