From 90379cfadeaa65092a4d1f695ef40775678859dc Mon Sep 17 00:00:00 2001 From: Mads Kristensen Date: Wed, 10 Aug 2022 08:01:17 -0700 Subject: [PATCH] Added overloads to moniker extension methods --- .../ImageMonikerExtensions.cs | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ImageMonikerExtensions.cs b/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ImageMonikerExtensions.cs index 3f62223..34eff3a 100644 --- a/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ImageMonikerExtensions.cs +++ b/src/toolkit/Community.VisualStudio.Toolkit.Shared/ExtensionMethods/ImageMonikerExtensions.cs @@ -34,15 +34,37 @@ public static class ImageMonikerExtensions return data as BitmapSource; } + /// + /// Converts an ImageMoniker to a bitmap in the specified size. + /// + public static async Task ToBitmapSourceAsync(this ImageMoniker moniker, int size, Color backgroundColor) + { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + + IVsUIObject? result = await ToUiObjectAsync(moniker, size, backgroundColor); + ErrorHandler.ThrowOnFailure(result.get_Data(out object data)); + + return data as BitmapSource; + } + /// /// Converts an ImageMoniker to an IVsUIObject in the specified size. /// public static async Task ToUiObjectAsync(this ImageMoniker moniker, int size) + { + Color backColor = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey); + + return await ToUiObjectAsync(moniker, size, backColor); + } + + /// + /// Converts an ImageMoniker to an IVsUIObject in the specified size with the specified background color. + /// + public static async Task ToUiObjectAsync(this ImageMoniker moniker, int size, Color backgroundColor) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); IVsImageService2 imageService = await VS.GetRequiredServiceAsync(); - Color backColor = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey); ImageAttributes imageAttributes = new() { @@ -52,7 +74,7 @@ public static async Task ToUiObjectAsync(this ImageMoniker moniker, Dpi = 96, LogicalHeight = size, LogicalWidth = size, - Background = (uint)backColor.ToArgb(), + Background = (uint)backgroundColor.ToArgb(), StructSize = Marshal.SizeOf(typeof(ImageAttributes)) };