Browse Source

Improve saving files when setting as wallpaper

Ruben 11 months ago
parent
commit
213e466775

+ 34 - 30
src/PicView.Avalonia/ImageHandling/ImageHelper.cs

@@ -19,16 +19,35 @@ public static class ImageHelper
     
     public static async Task<string> ConvertToCommonSupportedFormatAsync(string path, MainViewModel vm)
     {
-        if (NavigationHelper.CanNavigate(vm) && vm.EffectConfig is not null && !string.IsNullOrEmpty(path))
+        Bitmap? source = null;
+        
+        if (vm.EffectConfig is not null)
         {
-            Bitmap? source = null;
-            if (path == vm.FileInfo.FullName)
+            if (vm.ImageSource is Bitmap bmp)
+            {
+                source = bmp;
+            }
+        }
+        else if (NavigationHelper.CanNavigate(vm) && !string.IsNullOrEmpty(path))
+        {
+            if (vm.EffectConfig is not null && vm.FileInfo.FullName == path)
             {
                 if (vm.ImageSource is Bitmap bmp)
                 {
                     source = bmp;
                 }
             }
+            else if (path == vm.FileInfo.FullName)
+            {
+                if (path.IsCommon())
+                {
+                    return path;
+                }
+                if (vm.ImageSource is Bitmap bmp && vm.FileInfo.FullName.IsSupported())
+                {
+                    source = bmp;
+                }
+            }
             else
             {
                 var preloadValue = await vm.ImageIterator.GetPreLoadValueAsync(vm.ImageIterator.ImagePaths.IndexOf(path))
@@ -38,14 +57,15 @@ public static class ImageHelper
                     source = bitmap;
                 }
             }
-
-            if (source is not null)
-            {
-                var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + "bmp");
-                source.Save(tempPath);
-                return tempPath;
-            }
         }
+        var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".png");
+        
+        if (source is not null)
+        {
+            await Task.Run(() => source.Save(tempPath));
+            return tempPath;
+        }
+        
         var url = path.GetURL();
         if (!string.IsNullOrWhiteSpace(url))
         {
@@ -65,26 +85,10 @@ public static class ImageHelper
         if (!File.Exists(path))
             return string.Empty;
         
-        var ext = Path.GetExtension(path).ToLower();
-        switch (ext)
-        {
-            case ".gif": // Don't know what to do if animated?
-            case ".png":
-            case ".jpg":
-            case ".jpeg":
-            case ".jpe":
-            case ".bmp":
-            case ".jfif":
-                return path;
-
-            default:
-                var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ext);
-                // Save image to temp path
-                var success = await SaveImageFileHelper.SaveImageAsync(null, path, tempPath, null, null, null, ".png");
-                return !success ? string.Empty : tempPath;
-        }
-        
-
+        // Convert the image to a common supported format
+        // Save image to temp path
+        var success = await SaveImageFileHelper.SaveImageAsync(null, path, tempPath, null, null, null, ".png");
+        return !success ? string.Empty : tempPath;
     }
     
     

+ 9 - 0
src/PicView.Core/FileHandling/SupportedFiles.cs

@@ -103,6 +103,15 @@ public static class SupportedFiles
         ".cab"
     ];
 
+    public static bool IsCommon(this string file)
+    {
+        return Path.GetExtension(file).ToLower() switch
+        {
+            ".jpg" or ".jpeg" or ".png" or ".bmp" or ".gif" or ".jfif" => true,
+            _ => false
+        };
+    }
+
     public static List<string> ConvertArchivesToGlobFormat()
     {
         return FileExtensionsArchives.Select(ext => $"*{ext}").ToList();