1
1
Эх сурвалжийг харах

Check frames before starting XAMLAnimatedGif

Ruben 2 жил өмнө
parent
commit
c21609686d

+ 6 - 2
src/PicView/ChangeImage/UpdateImage.cs

@@ -78,8 +78,12 @@ internal static class UpdateImage
 
 
         if (preLoadValue.FileInfo.Extension.Equals(".gif", StringComparison.OrdinalIgnoreCase))
         if (preLoadValue.FileInfo.Extension.Equals(".gif", StringComparison.OrdinalIgnoreCase))
         {
         {
-            var uri = new Uri(Pics?[index]);
-            AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, uri);
+            var frames = ImageFunctions.GetImageFrames(Pics[index]);
+            if (frames > 0)
+            {
+                var uri = new Uri(Pics[index]);
+                AnimationBehavior.SetSourceUri(ConfigureWindows.GetMainWindow.MainImage, uri);
+            }
         }
         }
 
 
         if (GetToolTipMessage is { IsVisible: true })
         if (GetToolTipMessage is { IsVisible: true })

+ 26 - 0
src/PicView/ImageHandling/ImageFunctions.cs

@@ -166,6 +166,32 @@ internal static class ImageFunctions
         }
         }
     }
     }
 
 
+    /// <summary>
+    /// Gets the number of frames in an image.
+    /// </summary>
+    /// <param name="file">The path to the image file.</param>
+    /// <returns>The number of frames in the image. Returns 0 if an error occurs.</returns>
+    /// <remarks>
+    /// This method uses the Magick.NET library to load the image and retrieve the frame count.
+    /// </remarks>
+    public static int GetImageFrames(string file)
+    {
+        try
+        {
+            // Using statement ensures proper disposal of resources.
+            using var magick = new MagickImageCollection(file);
+            return magick.Count;
+        }
+        catch (MagickException ex)
+        {
+            // Log the exception for debugging purposes.
+            Trace.WriteLine($"{nameof(GetImageFrames)} Exception \n{ex}");
+
+            // Return 0 in case of an error.
+            return 0;
+        }
+    }
+
     internal static RenderTargetBitmap ImageErrorMessage()
     internal static RenderTargetBitmap ImageErrorMessage()
     {
     {
         var brokenBitmapImage = (DrawingImage)Application.Current.Resources["BrokenDrawingImage"];
         var brokenBitmapImage = (DrawingImage)Application.Current.Resources["BrokenDrawingImage"];