C#上传图片同时生成缩略图,控制图片上传大小
2023-09-27 14:26:14 时间
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 }
相关文章
- 微信公众号开发C#系列-5、用户和用户组管理-支持同步
- A simple dynamic library implemented in C# 4.0 to deal with XML structure
- C#—Nhibernate使用教程
- 分享自制的C#和VB Code互转工具
- C#编程:通过文件路径获取文件名
- 深刻理解C#的传值调用和传引用调用
- C#三种判断数据库中取出的字段值是否为空(NULL) 的方法
- unity c# 设置http头Header
- Unity3d 着色器Shader的[Toggle]组件C#脚本控制问题解决方法
- Unity3d C# 实现纯鼠标平滑控制场景摄像头(相机)实现自由旋转、移动和围绕节点移动旋转等功能(含源码工程)
- C#学习-关键字Sealed(密封:密封类+密封函数)
- .NET(C#) .NET Framework中自带的泛型委托Action
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_发布者策略控制
- C#操作Office- Cannot find the interop type that matches the embedded interop type 'Microsoft.Office.Interop.Excel.Application'
- 2019-8-30-C#-从零开始写-SharpDx-应用-笔刷
- 2019-1-29-C#-Task.Run-和-Task.Factory.StartNew-区别
- C# json 转 xml 字符串
- C# 从零开始写 SharpDx 应用 控制台创建 Sharpdx 窗口
- C# 代码占用的空间