From db4a48ef7542c38862562cc818b18d4c8a3b0e31 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 12 Sep 2015 14:05:34 +0200 Subject: [PATCH] Allow loading bitmaps from stream in D2D. --- src/Gtk/Perspex.Cairo/CairoPlatform.cs | 6 ++++++ src/Perspex.SceneGraph/Media/Imaging/Bitmap.cs | 11 +++++++++++ .../Platform/IPlatformRenderInterface.cs | 8 ++++++++ .../Perspex.Direct2D1/Direct2D1Platform.cs | 6 ++++++ .../Perspex.Direct2D1/Media/Imaging/BitmapImpl.cs | 15 +++++++++++++++ 5 files changed, 46 insertions(+) diff --git a/src/Gtk/Perspex.Cairo/CairoPlatform.cs b/src/Gtk/Perspex.Cairo/CairoPlatform.cs index b6d14e60a1..6105ef3046 100644 --- a/src/Gtk/Perspex.Cairo/CairoPlatform.cs +++ b/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) diff --git a/src/Perspex.SceneGraph/Media/Imaging/Bitmap.cs b/src/Perspex.SceneGraph/Media/Imaging/Bitmap.cs index 64872d3ada..92ec2ac1b9 100644 --- a/src/Perspex.SceneGraph/Media/Imaging/Bitmap.cs +++ b/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); } + /// + /// Initializes a new instance of the class. + /// + /// The stream to read the bitmap from. + public Bitmap(Stream stream) + { + IPlatformRenderInterface factory = Locator.Current.GetService(); + PlatformImpl = factory.LoadBitmap(stream); + } + /// /// Initializes a new instance of the class. /// diff --git a/src/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs b/src/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs index a7ce8e2abf..ca40ec8137 100644 --- a/src/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs +++ b/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 /// The filename of the bitmap. /// An . IBitmapImpl LoadBitmap(string fileName); + + /// + /// Loads a bitmap implementation from a file.. + /// + /// The stream to read the bitmap from. + /// An . + IBitmapImpl LoadBitmap(Stream stream); } } diff --git a/src/Windows/Perspex.Direct2D1/Direct2D1Platform.cs b/src/Windows/Perspex.Direct2D1/Direct2D1Platform.cs index 0d04bf1994..31b0ff34a1 100644 --- a/src/Windows/Perspex.Direct2D1/Direct2D1Platform.cs +++ b/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); + } } } diff --git a/src/Windows/Perspex.Direct2D1/Media/Imaging/BitmapImpl.cs b/src/Windows/Perspex.Direct2D1/Media/Imaging/BitmapImpl.cs index e5347cc5d6..4572c09850 100644 --- a/src/Windows/Perspex.Direct2D1/Media/Imaging/BitmapImpl.cs +++ b/src/Windows/Perspex.Direct2D1/Media/Imaging/BitmapImpl.cs @@ -32,6 +32,21 @@ namespace Perspex.Direct2D1.Media } } + /// + /// Initializes a new instance of the class. + /// + /// The WIC imaging factory to use. + /// The stream to read the bitmap from. + 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); + } + } + /// /// Initializes a new instance of the class. ///