C# 获取图片的大小

获取图片的大小 单位M

 1 /// <summary>
 2         /// 获取图片的大小
 3         /// </summary>
 4         /// <returns></returns>
 5         public bool Get_imgSize(string ImgUrl, out string SizeMsg)
 6         {
 7             try
 8             {
 9                 SizeMsg = string.Empty;
10                 if (string.IsNullOrWhiteSpace(ImgUrl))
11                 {
12                     SizeMsg = "未获取到图片信息";
13                     return false;
14                 }
15                 if (ImgUrl.Contains("name"))
16                 {
17                     var imgs = Common.JsonHelper.JsonDeserialize<List<FileObj>>(ImgUrl);
18                     foreach (var item in imgs)
19                     {
20                         ImgUrl = item.url;
21                     }
22                 }
23                 //判断图片大小
24                 var domain = Common.ConfigHelper.Get("OSS_CDN_Domain");
25                 var sitepath = Common.ConfigHelper.Get("CDN_SitePath");
26                 ImgUrl = ImgUrl.Replace(domain, sitepath);
27 
28                 FileInfo fileinfo = new FileInfo(ImgUrl);
29                 var a = fileinfo.Length.ToString();
30                 double length = Convert.ToDouble(fileinfo.Length);
31                 double Size = length / 1024 / 1024;
32                 double fourM = 4;
33                 if (Size > fourM)
34                 {
35                     SizeMsg = "KV画面上传字段,不超过4MB/张";
36                     return false;
37                 }
38                 else
39                 {
40                     return true;
41                 }
42 
43 
44             }
45             catch (Exception e)
46             {
47                 SizeMsg = "图片_" + e.Message;
48                 return false;
49             }
50 
51         }

 

posted @ 2023-11-07 17:04  小易儿  阅读(79)  评论(0编辑  收藏  举报