限制上传图片大小和格式代码

 1         /// <summary>
 2         /// 限制上传图片大小和格式
 3         /// </summary>
 4         /// <param name="url">图片路径</param>
 5         /// <param name="width">图片宽度</param>
 6         /// <param name="heigth">图片高度</param>
 7         /// <param name="IsDel">是否删除图片</param>
 8         protected bool LimitImageSize(string url,int width,int heigth,bool IsDel)
 9         {
10             FileStream stream = new FileStream(Server.MapPath("./" + url), FileMode.Open, FileAccess.Read);
11             System.Drawing.Image tempimage = System.Drawing.Image.FromStream(stream, true);
12             string[] format = url.Split('.');
13             string fileext = format[3].ToString();
14             if (fileext.Equals("jpg") || fileext.Equals("JPG"))
15             {
16                 int imagewidth = tempimage.Width;
17                 int imageheight = tempimage.Height;
18                 if (width >= imagewidth && heigth >= imagewidth)
19                 {
20                     stream.Close();
21                     if (IsDel==true)
22                     {
23                         var ImageUrl = new FileInfo(Server.MapPath("./" + url));
24                         ImageUrl.Delete();
25                     }
26                     Alert.ShowInTop("图片应大于或等于350*240px");
27                     return true;
28                 }
29                 else
30                 {
31                     stream.Close();
32                     return false;
33                 }
34             }
35             else
36             {
37                 stream.Close();
38                 if (IsDel == true)
39                 {
40                     var ImageUrl = new FileInfo(Server.MapPath("./" + url));
41                     ImageUrl.Delete();
42                 }
43                 Alert.ShowInTop("图片应为JPG格式");
44                 return true;
45             }
46         }

posted on 2016-08-17 14:24  阿门668  阅读(1998)  评论(0编辑  收藏  举报

导航