关于图片缩放的c#方法

时间:2022-01-14 04:55:11

标签:   图片缩略图   

在网站建设中,关于图片是必不可少的,后台管理中往往需要上传图片,大的图片在网络中传输速率很慢,很不理想,因此解决办法是,用户上传图片时候,保存一个图片的缩略图,在网页显示用缩略图,用户下载,使用原图,,下面是通过搜索资料,整理的c#关于保存图片缩略图的方法,用户只需要传入适当参数,调用此方法就可以生成缩略图。

    using System;     using System.Collections.Generic;     using System.Linq;     using System.Web;     using System.Web.UI;     using System.Web.UI.WebControls;     using System.IO;        public partial class upload : System.Web.UI.Page      {              protected void Page_Load(object sender, EventArgs e)              {                                    }          }       /// <summary>          /// 图片等比缩放          /// </summary>          /// <param name="postedfile">原图地址加名称</param>          /// <param name="savepath">缩略图存放地址</param>          /// <param name="smallname">缩略图名称</param>          /// <param name="targetwidth">指定的最大宽度</param>          /// <param name="targetheight">指定的最大高度</param>          public static void zoomauto(string initpath, string savepath,string smallname, double targetwidth, double targetheight)          {                  //虚拟路径转绝对路径                  initpath = System.Web.HttpContext.Current.Server.MapPath(initpath);                  savepath = System.Web.HttpContext.Current.Server.MapPath(savepath);                  //创建目录                  string dir = Path.GetDirectoryName(savepath);                  if (!Directory.Exists(dir))                          Directory.CreateDirectory(dir);                    //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)                  System.Drawing.Image initimage = System.Drawing.Image.FromFile(initpath);           //原图宽高均小于模版,不作处理,直接保存                  if (initimage.Width <= targetwidth && initimage.Height <= targetheight)                  {                          //保存                          initimage.Save(savepath + smallname, System.Drawing.Imaging.ImageFormat.Jpeg);                  }                  else                  {                          //缩略图宽、高计算                          double newwidth = initimage.Width;                          double newheight = initimage.Height;               //宽大于高或宽等于高(横图或正方)                          if (initimage.Width > initimage.Height || initimage.Width == initimage.Height)                          {                                  //如果宽大于模版                                  if (initimage.Width > targetwidth)                  {                                          //宽按模版,高按比例缩放                                          newwidth = targetwidth;                                          newheight = initimage.Height * (targetwidth / initimage.Width);                                  }                          }                          //高大于宽(竖图)                          else                          {                                  //如果高大于模版                                  if (initimage.Height > targetheight)                                  {                                          //高按模版,宽按比例缩放                                          newheight = targetheight;                                          newwidth = initimage.Width * (targetheight / initimage.Height);                                  }                          }                            //生成新图                          //新建一个bmp图片                          System.Drawing.Image newimage = new System.Drawing.Bitmap((int)newwidth, (int)newheight);                          //新建一个画板                          System.Drawing.Graphics newg = System.Drawing.Graphics.FromImage(newimage);               //设置质量                          newg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                          newg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;               //置背景色                          newg.Clear(System.Drawing.Color.White);                          //画图                          newg.DrawImage(initimage, new System.Drawing.Rectangle(0, 0, newimage.Width, newimage.Height), new System.Drawing.Rectangle(0, 0, initimage.Width, initimage.Height), System.Drawing.GraphicsUnit.Pixel);               //保存缩略图                          newimage.Save(savepath + smallname, System.Drawing.Imaging.ImageFormat.Jpeg);               //释放资源                          newg.Dispose();                          newimage.Dispose();                          initimage.Dispose();                   }          }   }