Browse Source

Fix for exception: You cannot call the Save method on an Encoder more than once.

tomaszkot 3 years ago
parent
commit
bb827e76c5
1 changed files with 7 additions and 7 deletions
  1. 7 7
      PixiEditor/Models/IO/Exporter.cs

+ 7 - 7
PixiEditor/Models/IO/Exporter.cs

@@ -54,7 +54,7 @@ namespace PixiEditor.Models.IO
             {
                 var chosenFormat = ParseImageFormat(Path.GetExtension(path));
                 var bitmap = document.Renderer.FinalBitmap;
-                SaveAs(encodersFactory[chosenFormat], path, bitmap.PixelWidth, bitmap.PixelHeight, bitmap);
+                SaveAs(encodersFactory[chosenFormat](), path, bitmap.PixelWidth, bitmap.PixelHeight, bitmap);
             }
             else
             {
@@ -69,14 +69,14 @@ namespace PixiEditor.Models.IO
             return SupportedFilesHelper.ParseImageFormat(extension);
         }
 
-        static Dictionary<FileType, BitmapEncoder> encodersFactory = new Dictionary<FileType, BitmapEncoder>();
+        static Dictionary<FileType, Func<BitmapEncoder>> encodersFactory = new Dictionary<FileType, Func<BitmapEncoder>>();
 
         static Exporter()
         {
-            encodersFactory[FileType.Png] = new PngBitmapEncoder();
-            encodersFactory[FileType.Jpeg] = new JpegBitmapEncoder();
-            encodersFactory[FileType.Bmp] = new BmpBitmapEncoder(); 
-            encodersFactory[FileType.Gif] = new GifBitmapEncoder();
+            encodersFactory[FileType.Png] = () => new PngBitmapEncoder();
+            encodersFactory[FileType.Jpeg] = () => new JpegBitmapEncoder();
+            encodersFactory[FileType.Bmp] = () => new BmpBitmapEncoder(); 
+            encodersFactory[FileType.Gif] = () => new GifBitmapEncoder();
         }
 
         /// <summary>
@@ -92,7 +92,7 @@ namespace PixiEditor.Models.IO
           if (info.ShowDialog())
           {
             if(encodersFactory.ContainsKey(info.ChosenFormat))
-              SaveAs(encodersFactory[info.ChosenFormat], info.FilePath, info.FileWidth, info.FileHeight, bitmap);
+              SaveAs(encodersFactory[info.ChosenFormat](), info.FilePath, info.FileWidth, info.FileHeight, bitmap);
           }
         }
         public static void SaveAsGZippedBytes(string path, Surface surface)