首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么实现下传图片生成大小缩略图

2012-10-29 
如何实现上传图片生成大小缩略图要这个功能我是要做产品展示用的,想在上传大的图片时生成一个缩略图放在首

如何实现上传图片生成大小缩略图
要这个功能我是要做产品展示用的,想在上传大的图片时生成一个缩略图放在首页上显示,主要教我怎么生成缩略图然后上传大图和缩略图到相应的目录就可以,添加到数据库的我再弄,请各位大侠帮帮忙,希望代码能有注释,谢谢了!

[解决办法]
方法2

C# code
public void MakeThumbnail(string imgPath_old, int width, int height){   System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(imgPath_old)));   int towidth = width; int toheight = height;  int x = 0; int y = 0; int ow = img.Width;  int oh = img.Height;  // 按值较大的进行等比缩放(不变形)   if ((double)img.Width / (double)towidth < (double)img.Height / (double)toheight)  {    toheight = height;    towidth = img.Width * height / img.Height;  }  else  {    towidth = width;    toheight = img.Height * width / img.Width;  }  //新建一个bmp图片  System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);  //新建一个画板  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(img, new System.Drawing.Rectangle(0, 0, towidth, toheight),  new System.Drawing.Rectangle(x, y, ow, oh),  System.Drawing.GraphicsUnit.Pixel);  String newFile = Server.MapPath("~/small_" + Path.GetFileName(imgPath_old));  bitmap.Save(newFile , System.Drawing.Imaging.ImageFormat.Jpeg);  bitmap.Dispose();  img.Dispose();  g.Dispose();} protected void Page_Load(object sender, EventArgs e){  MakeThumbnail(Server.MapPath("~/aaa.jpg"), 100, 100);} 

热点排行