Browse Source

Allow loading bitmaps from stream in D2D.

Steven Kirk 10 years ago
parent
commit
db4a48ef75

+ 6 - 0
src/Gtk/Perspex.Cairo/CairoPlatform.cs

@@ -10,6 +10,7 @@ using Splat;
 
 namespace Perspex.Cairo
 {
+    using System.IO;
     using global::Cairo;
 
     public class CairoPlatform : IPlatformRenderInterface
@@ -60,6 +61,11 @@ namespace Perspex.Cairo
             return new BitmapImpl(result);
         }
 
+        public IBitmapImpl LoadBitmap(Stream stream)
+        {
+            throw new NotImplementedException();
+        }
+
         private Pango.Context GetPangoContext(IPlatformHandle handle)
         {
             switch (handle.HandleDescriptor)

+ 11 - 0
src/Perspex.SceneGraph/Media/Imaging/Bitmap.cs

@@ -1,6 +1,7 @@
 // Copyright (c) The Perspex Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
+using System.IO;
 using Perspex.Platform;
 using Splat;
 
@@ -21,6 +22,16 @@ namespace Perspex.Media.Imaging
             PlatformImpl = factory.LoadBitmap(fileName);
         }
 
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Bitmap"/> class.
+        /// </summary>
+        /// <param name="stream">The stream to read the bitmap from.</param>
+        public Bitmap(Stream stream)
+        {
+            IPlatformRenderInterface factory = Locator.Current.GetService<IPlatformRenderInterface>();
+            PlatformImpl = factory.LoadBitmap(stream);
+        }
+
         /// <summary>
         /// Initializes a new instance of the <see cref="Bitmap"/> class.
         /// </summary>

+ 8 - 0
src/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs

@@ -1,6 +1,7 @@
 // Copyright (c) The Perspex Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
+using System.IO;
 using Perspex.Media;
 
 namespace Perspex.Platform
@@ -65,5 +66,12 @@ namespace Perspex.Platform
         /// <param name="fileName">The filename of the bitmap.</param>
         /// <returns>An <see cref="IBitmapImpl"/>.</returns>
         IBitmapImpl LoadBitmap(string fileName);
+
+        /// <summary>
+        /// Loads a bitmap implementation from a file..
+        /// </summary>
+        /// <param name="stream">The stream to read the bitmap from.</param>
+        /// <returns>An <see cref="IBitmapImpl"/>.</returns>
+        IBitmapImpl LoadBitmap(Stream stream);
     }
 }

+ 6 - 0
src/Windows/Perspex.Direct2D1/Direct2D1Platform.cs

@@ -2,6 +2,7 @@
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
 using System;
+using System.IO;
 using Perspex.Direct2D1.Media;
 using Perspex.Media;
 using Perspex.Platform;
@@ -72,5 +73,10 @@ namespace Perspex.Direct2D1
         {
             return new BitmapImpl(s_imagingFactory, fileName);
         }
+
+        public IBitmapImpl LoadBitmap(Stream stream)
+        {
+            return new BitmapImpl(s_imagingFactory, stream);
+        }
     }
 }

+ 15 - 0
src/Windows/Perspex.Direct2D1/Media/Imaging/BitmapImpl.cs

@@ -32,6 +32,21 @@ namespace Perspex.Direct2D1.Media
             }
         }
 
+        /// <summary>
+        /// Initializes a new instance of the <see cref="BitmapImpl"/> class.
+        /// </summary>
+        /// <param name="factory">The WIC imaging factory to use.</param>
+        /// <param name="stream">The stream to read the bitmap from.</param>
+        public BitmapImpl(ImagingFactory factory, Stream stream)
+        {
+            _factory = factory;
+
+            using (BitmapDecoder decoder = new BitmapDecoder(factory, stream, DecodeOptions.CacheOnLoad))
+            {
+                WicImpl = new Bitmap(factory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad);
+            }
+        }
+
         /// <summary>
         /// Initializes a new instance of the <see cref="BitmapImpl"/> class.
         /// </summary>