记录一下以前的code(现在看起来当时的代码是这么写出来的,哈哈,自嘲一下),可以结合minio等obs文件服务器
// 在ASP.NET中上传图片并生成缩略图 //检查上传文件的格式是否有效 if (this.fileupload.PostedFile.ContentType.ToLower().IndexOf("image") < 0) { ScriptManager.RegisterStartupScript(updatepanel, GetType(), "", "alert('上传图片格式无效')", true); return; } if (selectTime.Text.Trim().Length <= 0) { ScriptManager.RegisterStartupScript(updatepanel, GetType(), "", "alert('请选择图片的分类日期')", true); return; } if (fileupload.HasFile) { string imgPath = "/StudentShow/StudentImgs/" + DateTime.Now.Year; string teximgTitleStr = Path.GetFileName(fileupload.PostedFile.FileName);// 图片title 类似:2D5A7274-2.jpg if (HaveUploadImg(teximgTitleStr))//去掉重复上传图片的方法 { ScriptManager.RegisterStartupScript(updatepanel, GetType(), "", "alert('此张图片已经上传,\\r\\n\\n请不要重复操作哦!')", true); return; } if (!Directory.Exists(Server.MapPath(imgPath))) { Directory.CreateDirectory(Server.MapPath(imgPath)); } string imgfileName = fileupload.PostedFile.FileName; string imgEx = imgfileName.Substring(imgfileName.LastIndexOf(".")); if (imgEx.ToLower() != ".png" && imgEx.ToLower() != ".jpg" && imgEx.ToLower() != ".gif") { ScriptManager.RegisterStartupScript(updatepanel, GetType(), "", "alert('非法的图片格式')", true); return; } float imgsize = ((float)(fileupload.PostedFile.ContentLength * 1.0) / 1024 / 1024); if (imgsize > 2.2) { ScriptManager.RegisterStartupScript(updatepanel, GetType(), "", "alert('上传的图片过大,最大建议2M以下,谢谢')", true); return; } //生成原图 // Byte[] oFileByte = new byte[this.fileupload.PostedFile.ContentLength]; System.IO.Stream oStream = this.fileupload.PostedFile.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width; //原图宽度 int oHeight = oImage.Height; //原图高度 int tWidth = 180; //设置缩略图初始宽度 int tHeight = 140; //设置缩略图初始高度 //按比例计算出缩略图的宽度和高度 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 timeStrb = "b" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";//原图大图并为jpg的格式名称 //可以提出来 string timeStrs = "s" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";//保存为小图并为jpg的格式名称 // string imgPath = "/StudentShow/StudentImgs/" + DateTime.Now.Year; string oFullName = Server.MapPath(imgPath) + "/" + timeStrb; //保存原图的物理路径 string tFullName = Server.MapPath(imgPath) + "/" + timeStrs; //保存缩略图的物理路径 int flagstr = GetImgCountByIsdel0();//获取img的flag,方便点击图片来回切换上下图片 try { //1 保存到数据库 string sqlstr = "insert into sumStudentImg(BimgPath,title,addTime,isDel,className,autoTime,SimgPath,flag) values(@BimgPath,@title,@addTime,@isDel,@className,@autoTime,@SimgPath,@flag)"; if (Mysqlhelper.ExecuterNoquery(sqlstr, System.Data.CommandType.Text, new MySqlParameter[]{ new MySqlParameter("@BimgPath", DateTime.Now.Year+"/"+timeStrb), new MySqlParameter("@title",teximgTitleStr),//上传图片的名称 new MySqlParameter("@addTime",selectTime.Text), new MySqlParameter("@isDel",'0'), new MySqlParameter("@className",droplistClass.SelectedValue), new MySqlParameter("@autoTime",DateTime.Now), new MySqlParameter("@SimgPath", DateTime.Now.Year+"/"+timeStrs), new MySqlParameter("@flag",flagstr) }) > 0) { //2以JPG格式保存图片 oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg); tImage.Save(tFullName, System.Drawing.Imaging.ImageFormat.Jpeg); // ScriptManager.RegisterStartupScript(updatepanel, GetType(), "", "alert('恭喜上传成功ok')", true); showUploadImgOk.Visible = true; } } catch (Exception ex) { ScriptManager.RegisterStartupScript(updatepanel, GetType(), "", "alert('图片上传出错,原因:" + ex.Message + "')", true); } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } }
标签:code,MySqlParameter,缩略图,int,System,new,上传 From: https://www.cnblogs.com/Fengge518/p/17640722.html