Theming ToolStrip buttons using Image service

Sajith 66 Reputation points
2025-11-07T16:21:43.1966667+00:00

I am trying to use images for ToolStrip buttons using Image service following this-

https://learn.microsoft.com/en-us/visualstudio/extensibility/image-service-and-catalog?view=vs-2022

But this is not working with changing themes. I have tried setting the image on ThemeChanged event but that does not help.

I just need to get a button like this (KnownMonikers.ClearWindowContent) working in different themes –

User's image

public void UpdateToolstripTheme()
    {
      IVsImageService2 imageService = (IVsImageService2)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsImageService));
      ImageAttributes attributes = new ImageAttributes
      {
        StructSize = Marshal.SizeOf(typeof(ImageAttributes)),
        ImageType = (uint)_UIImageType.IT_Bitmap,
        Format = (uint)_UIDataFormat.DF_WinForms,
        Dpi = (int)DpiAwareness.GetWindowDpi(m_Toolbar.Handle),
        LogicalWidth = 16,
        LogicalHeight = 16,
        Background = 0xFFFFFFFF,
        Flags = unchecked((uint)(_ImageAttributesFlags.IAF_RequiredFlags | _ImageAttributesFlags.IAF_Background))
      };
      IVsUIObject stopUiObj = imageService.GetImage(KnownMonikers.Stop, attributes);
      IVsUIObject clearUiObj = imageService.GetImage(KnownMonikers.ClearWindowContent, attributes);
 
      Bitmap stopBitmap = (Bitmap)GelUtilities.GetObjectData(stopUiObj);
      Bitmap clearBitmap = (Bitmap)GelUtilities.GetObjectData(clearUiObj);
      m_ToolbarBtnStop.Image = stopBitmap;
      m_ToolbarBtnClear.Image = clearBitmap;
    }

This works fine in light theme. But on changing to dark theme, the images do not display correctly.

Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

Answer accepted by question author
  1. Gade Harika (INFOSYS LIMITED) 2,020 Reputation points Microsoft External Staff
    2025-11-10T08:25:29.1+00:00

    Thanks for reaching out.

    The issue occurs because the image attributes are forcing a white background, which doesn’t adapt to theme changes. To fix this:

    • Remove the hardcoded Background value.
    • Add _ImageAttributesFlags.IAF_Theme to the Flags so the Image Service applies theme-based rendering.

    Here’s an updated code snippet:

    Flags = unchecked((uint)(_ImageAttributesFlags.IAF_RequiredFlags | _ImageAttributesFlags.IAF_Theme))
    

    This ensures your ToolStrip buttons display correctly in both light and dark themes. Let me know if you need a full working example or guidance on handling ThemeChanged events.

    Let us know if the issue persists after following these steps. I’ll be happy to assist further if needed. If the issue has been resolved, Kindly mark the provided solution as "Accept Answer", so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.