Ver código fonte

fix image 檔尾毀損時異常處理

JAELYS 3 anos atrás
pai
commit
e1f201c1ff
1 arquivos alterados com 14 adições e 6 exclusões
  1. 14 6
      TEAMModelOS.SDK/Extension/Utils.cs

+ 14 - 6
TEAMModelOS.SDK/Extension/Utils.cs

@@ -109,10 +109,18 @@ namespace TEAMModelOS.SDK.Extension
             StringBuilder stringBuilder = new StringBuilder();
             for (int i = 0; i < length; i++)
             {
-                byte tempByte = br.ReadByte();
-                bytes[i] = tempByte;
-                stringBuilder.Append(Convert.ToString(tempByte, 16));
-                stringBuilder.Append(",");
+                byte tempByte;
+                try
+                {
+                    tempByte = br.ReadByte();
+                    bytes[i] = tempByte;
+                    stringBuilder.Append(Convert.ToString(tempByte, 16));
+                    stringBuilder.Append(",");
+                }
+                catch 
+                {
+                    break; //如果發生檔尾損壞異常抱錯,直接跳離並結算stringBuilder
+                }               
             }
             stream.Position = 0; //指針回歸為0
 
@@ -145,7 +153,7 @@ namespace TEAMModelOS.SDK.Extension
 
                 return (true, "png", depth);
             }
-            
+
             if (fileheader.StartsWith("47,49,46,38,39,61,") || fileheader.StartsWith("47,49,46,38,37,61,"))
                 return (true, "gif", 0);
 
@@ -165,7 +173,7 @@ namespace TEAMModelOS.SDK.Extension
         /// </summary>
         /// <param name="stream"></param>
         /// <returns></returns>
-        public static string ConvertEMFtoPNG(Stream stream) 
+        public static string ConvertEMFtoPNG(Stream stream)
         {
             using var image = Image.FromStream(stream);