C#上传图片同时生成缩略图,控制图片上传大小。

  1     #region 上传图片生成缩略图
  2     /// <summary>
  3     /// 上传图片
  4     /// </summary>
  5     /// <param name="sender"></param>
  6     /// <param name="e"></param>
  7     public void btnUpload_Click(object sender, EventArgs e)
  8     {
  9         //检查上传文件的格式是否有效 
 10         if (this.fudPhoto.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
 11         {
 12             Response.Write("<script>alert('上传图片格式无效!');window.location.href='LES_ApplyTopActivityAdd.aspx';</script>");
 13             return;
 14         }
 15         string FileNmae;
 16         string FileNameS;
 17         string name = fudPhoto.PostedFile.FileName;//客户端文件路径
 18         FileInfo file = new FileInfo(name);
 19         string fileName = file.Name;
 20         string ExtName = getFileExt(fileName).ToUpper();
 21         //生成原图 
 22         Byte[] oFileByte = new byte[this.fudPhoto.PostedFile.ContentLength];
 23         System.IO.Stream oStream = this.fudPhoto.PostedFile.InputStream;
 24         System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);
 25         int towidth = 140;//设置缩略图初始高度
 26         int toheight = 140;//设置缩略图初始宽度 
 27         int x = 0;
 28         int y = 0;
 29         int ow = oImage.Width;
 30         int oh = oImage.Height;
 31 
 32         if (ow == 640 && ow == 640)
 33         {
 34             //按比例计算出缩略图的宽度和高度 
 35 
 36             if (((double)oImage.Width / (double)oImage.Height) > ((double)towidth / (double)toheight))
 37             {
 38                 oh = oImage.Height;
 39                 ow = oImage.Height * towidth / toheight;
 40                 y = 0;
 41                 x = (oImage.Width - ow) / 2;
 42             }
 43             else
 44             {
 45                 ow = oImage.Width;
 46                 oh = oImage.Width * toheight / towidth;
 47                 x = 0;
 48                 y = (oImage.Height - oh) / 2;
 49             }
 50             //生成缩略原图 
 51             Bitmap tImage = new Bitmap(towidth, toheight);
 52             Graphics g = Graphics.FromImage(tImage);
 53             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 
 54             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 
 55             g.Clear(Color.Transparent); //清空画布并以透明背景色填充 
 56             g.DrawImage(oImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(0, 0, ow, oh), GraphicsUnit.Pixel);
 57 
 58             FileNmae = DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "." + ExtName; //保存原图的物理路径 
 59             string oFullName = Server.MapPath("/uploadfiles//" + FileNmae);
 60 
 61             FileNameS = FileNmae + "-S" + ".JPG"; //保存缩略图的物理路径
 62             string tFullName = Server.MapPath("/uploadfiles//" + FileNameS);
 63             try
 64             {
 65                 //以JPG格式保存图片 
 66                 oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
 67                 tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
 68 
 69                 //lbMessage.Text = "提示:文件”" + FileNmae + "“成功上传,并生成”" + FileNameS + "“缩略图,文件类型为:" +
 70                 //     fudPhoto.PostedFile.ContentType + ",文件大小为:" + fudPhoto.PostedFile.ContentLength + "B";
 71                 lbMessage.Text = "提示:文件成功上传,并生成”" + FileNameS + "“缩略图!";
 72                 string PIC = FileNmae;
 73                 ViewState["pic"] = PIC;
 74             }
 75             catch (Exception ex)
 76             {
 77                 throw ex;
 78             }
 79             finally
 80             {
 81                 //释放资源 
 82                 oImage.Dispose();
 83                 g.Dispose();
 84                 tImage.Dispose();
 85             }
 86         }
 87         else
 88         {
 89             lbMessage.Text = "提示:图片的尺寸应该是640x640";
 90         }
 91     }
 92     private string getFileExt(string fileName)
 93     {
 94         if (fileName.IndexOf(".") == -1)
 95             return "";
 96         string[] temp = fileName.Split('.');
 97         return temp[temp.Length - 1].ToLower();
 98     }
 99     #endregion
100 }
如图效果图:


原文地址:https://www.cnblogs.com/hulxm/p/5053573.html