excelvstoexcel-addins

How to Increase ribbon button image size in vsto Excel addin


I have built a VSTO Excel addin with a simple button. I added an image to the button, but the image size in excel is always small:

enter image description here

I cant find a way to increase the image size, I would like the entire ribbon item to be the image, eg

enter image description here

The image size properties for the button are greyed out in the designer. I tried add the image programmatically on load but the size always reverts to small. Does anyone have any idea how to make a ribbon button image take up the entire ribbon area?

I use this code to load the button image:

 private void CustomRibbon_Load(object sender, RibbonUIEventArgs e)
  {
          
      string imgName = "Refresh.jpg";
      string path = AppDomain.CurrentDomain.BaseDirectory; // System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
      string fileName = path + (path.EndsWith("\\") ? "" : "\\") + imgName;
      Image bm = new Bitmap(fileName);
      Image bm2 = new Bitmap(bm, 300, 300);
     

      btnRefresh.Image = bm2;

  }

Thanks!


Solution

  • In VSTO, the default size for ribbon button images is set by the RibbonControlSize property. From the help file:

    private void SetButtonProperties()

    {
        button1.ControlSize =
            Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
        button1.Description = "My Ribbon Button";
    }
    

    In your case the following should work:

    private void CustomRibbon_Load(object sender, RibbonUIEventArgs e)
    {
        string imgName = "Refresh.jpg";
        string path = AppDomain.CurrentDomain.BaseDirectory;
        string fileName = System.IO.Path.Combine(path, imgName);
    
        if (System.IO.File.Exists(fileName))
        {
            // Load and scale the image (should be around 32x32 pixels for large size)
            Image bm = new Bitmap(fileName);
            Image bmScaled = new Bitmap(bm, new Size(32, 32));  // Scale to 32x32
    
            btnRefresh.Image = bmScaled;
            btnRefresh.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
            btnRefresh.ShowImage = true;
        }
    }