Skip to content

Commit

Permalink
Merge branch 'VsixCommunity:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertvanderHulst authored Sep 14, 2022
2 parents 6798dea + 90379cf commit 3fb426b
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,37 @@ public static class ImageMonikerExtensions
return data as BitmapSource;
}

/// <summary>
/// Converts an ImageMoniker to a bitmap in the specified size.
/// </summary>
public static async Task<BitmapSource?> 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;
}

/// <summary>
/// Converts an ImageMoniker to an IVsUIObject in the specified size.
/// </summary>
public static async Task<IVsUIObject> ToUiObjectAsync(this ImageMoniker moniker, int size)
{
Color backColor = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey);

return await ToUiObjectAsync(moniker, size, backColor);
}

/// <summary>
/// Converts an ImageMoniker to an IVsUIObject in the specified size with the specified background color.
/// </summary>
public static async Task<IVsUIObject> ToUiObjectAsync(this ImageMoniker moniker, int size, Color backgroundColor)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

IVsImageService2 imageService = await VS.GetRequiredServiceAsync<SVsImageService, IVsImageService2>();
Color backColor = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey);

ImageAttributes imageAttributes = new()
{
Expand All @@ -52,7 +74,7 @@ public static async Task<IVsUIObject> ToUiObjectAsync(this ImageMoniker moniker,
Dpi = 96,
LogicalHeight = size,
LogicalWidth = size,
Background = (uint)backColor.ToArgb(),
Background = (uint)backgroundColor.ToArgb(),
StructSize = Marshal.SizeOf(typeof(ImageAttributes))
};

Expand Down

0 comments on commit 3fb426b

Please sign in to comment.