Browse Source

Implemented preview images

CPKreuz 2 years ago
parent
commit
173bc4c370

+ 20 - 4
src/PixiEditor/Models/DataHolders/RecentlyOpenedDocument.cs

@@ -73,17 +73,33 @@ internal class RecentlyOpenedDocument : NotifyableObject
     {
         if (FileExtension == ".pixi")
         {
-            
             SerializableDocument serializableDocument;
 
             try
             {
-                serializableDocument = DepractedPixiParser.Deserialize(filePath);
+                var document = PixiParser.Deserialize(filePath);
+
+                if (document.PreviewImage == null || document.PreviewImage.Length == 0)
+                {
+                    return null;
+                }
+                
+                using var data = new MemoryStream(document.PreviewImage);
+                var decoder = new PngBitmapDecoder(data, BitmapCreateOptions.None, BitmapCacheOption.OnDemand);
+                return new WriteableBitmap(decoder.Frames[0]);
             }
             catch
             {
-                corrupt = true;
-                return null;
+
+                try
+                {
+                    serializableDocument = DepractedPixiParser.Deserialize(filePath);
+                }
+                catch
+                {
+                    corrupt = true;
+                    return null;
+                }
             }
 
             using Surface surface = Surface.Combine(serializableDocument.Width, serializableDocument.Height,

+ 4 - 2
src/PixiEditor/ViewModels/SubViewModels/Document/DocumentViewModel.Serialization.cs

@@ -1,4 +1,6 @@
 using System.Drawing;
+using System.IO;
+using System.Windows.Media.Imaging;
 using ChunkyImageLib;
 using ChunkyImageLib.DataHolders;
 using PixiEditor.ChangeableDocument.Changeables.Interfaces;
@@ -24,12 +26,12 @@ internal partial class DocumentViewModel
         IReadOnlyDocument doc = Internals.Tracker.Document;
 
         AddMembers(doc.StructureRoot.Children, doc, root);
-        
+
         var document = new PixiDocument
         {
             Width = Width, Height = Height,
             Swatches = ToCollection(Swatches), Palette = ToCollection(Palette),
-            RootFolder = root
+            RootFolder = root, PreviewImage = PreviewSurface.Snapshot().Encode().AsSpan().ToArray()
         };
 
         return document;