明霞山资源网 Design By www.htccd.com

本文实例讲述了ASP.NET实现上传图片并生成缩略图的方法。分享给大家供大家参考,具体如下:

protected void bt_upload_Click(object sender, EventArgs e)
{
  //检查上传文件的格式是否有效 
  if (this.UploadFile.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
  {
   Response.Write("上传图片格式无效!");
   return;
  }
  //生成原图 
  Byte[] oFileByte = new byte[this.UploadFile.PostedFile.ContentLength];
  System.IO.Stream oStream = this.UploadFile.PostedFile.InputStream;
  System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
  int oWidth = oImage.Width; //原图宽度 
  int oHeight = oImage.Height; //原图高度 
  int tWidth = 100; //设置缩略图初始宽度 
  int tHeight = 100; //设置缩略图初始高度 
  //按比例计算出缩略图的宽度和高度 
  if (oWidth >= oHeight)
  {
   tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
  }
  else
  {
   tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
  }
  //生成缩略原图 
  Bitmap tImage = new Bitmap(tWidth, tHeight);
  Graphics g = Graphics.FromImage(tImage);
  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 
  g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 
  g.Clear(Color.Transparent); //清空画布并以透明背景色填充 
  g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
  string oFullName = Server.MapPath(".") + "/image/" + "o" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原图的物理路径 
  string tFullName = Server.MapPath(".") + "/image/" + "t" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径 
  try
  {
   //以JPG格式保存图片 
   oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
   tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
  }
  catch (Exception ex)
  {
   throw ex;
  }
  finally
  {
   //释放资源 
   oImage.Dispose();
   g.Dispose();
   tImage.Dispose();
  }
 }
}

这里再补充一个改进方法:

#region 上传图片 并生成缩略图
/// <summary>
/// 上传图片生成缩略图
/// </summary>
/// <param name="originalImagePath">图片源路径</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) {
  //从路径中获取源图片的文件
  System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  int towidth = width;
  int toheight = height;
  int x = 0;
   int y = 0;
  //获取图片的宽度
   int ow = image.Width;
  //获取图片的高度
   int oh = image.Height;
  //生成缩略图的方式
   switch (mode) { 
    case "HW":
    break;
    case "W"://指定宽度 高按比例
    toheight = originalImage.Height * width / originalImage.Width;
    break;
    case "H"://指定图片的高度 宽按比例
    towidth = originalImage.Width * height / originalImage.Height;
    break;
    case "Cut"://如果为裁减模式 则不变形 
    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
    {
     oh = originalImage.Height;
     //缩略图片的宽度
     ow = originalImage.Height * towidth / toheight;
     y = 0;
     x = (originalImage.Width - ow) / 2;
    }
    else {
     ow = originalImage.Width;
     //缩略图片的高度
     oh = originalImage.Width * toheight / towidth;
     x = 0;
     y(originalImage.Height - oh) / 2;
    }
    break;
    default: break;
   }
  //新建一个bmp图片
   Bitmap bitmap = new Bitmap(towidth, toheight);
  //新建一个画布 以BitMap 宽 高作为画布的大小
   Graphics g = Graphics.FromImage(bitmap);
  //设置高质量插值法
   g.InterpolationMode = InterpolationMode.High;
  //以高质量 低速度 呈现
   g.SmoothingMode = SmoothingMode.HighQuality;
  //清空画布 以白色背景色填充 
   g.Clear(Color.Transparent);
   //在指定位置并且按指定大小绘制原图片的指定部分
   g.DrawImage(originalImage,new Rectangle(towidth,toheight),new Rectangle(x,y,ow,oh),GraphicsUnit.Pixel);
   try
   {
    //以jpg格式保存缩略图
    bitmap.Save(thumbnailPath,System.Drawing.Imaging.ImageFormat.Jpeg);
   }
   catch (Exception ex)
   {
    throw ex;
   }
   finally {
    //释放资源
    originalImage.Dispose();
    bitmap.Dispose();
    g.Dispose();
   }
}
#endregion

希望本文所述对大家asp.net程序设计有所帮助。

标签:
ASP.NET,上传图片,生成缩略图,ASP.NET上传图片,ASP.NET生成缩略图

明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。