最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Increase ribbon button image size in vsto Excel addin - Stack Overflow

programmeradmin1浏览0评论

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:

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

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!

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:

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

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!

Share Improve this question edited Feb 4 at 5:35 Programnik asked Feb 4 at 4:48 ProgramnikProgramnik 1,5651 gold badge10 silver badges15 bronze badges 3
  • You need to provide an image of the appropriate size (32 pixels). Please include the code that returns the image. – Dmitry Streblechenko Commented Feb 4 at 5:17
  • I edited the question to show code I am using to load the image. The raw source image is large. – Programnik Commented Feb 4 at 5:35
  • I think you need to use Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge to set the required size. learn.microsoft/en-us/dotnet/api/… – Michal Commented Feb 4 at 9:03
Add a comment  | 

1 Answer 1

Reset to default 1

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论