Sometimes we want to use embedded image resource for ribbon button, so we can simplify our deliverables when release the add-ins (don’t need to copy external image files with the assembly). And actually we have the choice, following code will help you know how:
using System;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
namespace myRevitApp
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class MyApp : IExternalApplication
{
// ExternalCommands assembly path
static string addInPath = typeof(MyApp).Assembly.Location;
// Button icons directory
static string buttonIconsFolder = Path.GetDirectoryName(addInPath);
public Result OnStartup(UIControlledApplication uiControlledApplication)
{
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
// begin to create custom Ribbon panel and command buttons.
// create a Ribbon panel.
RibbonPanel myPanel = uiControlledApplication.CreateRibbonPanel(“My Ribbon Panel”);
// the first button in the DoorSwing panel, use to invoke the InitializeCommand.
PushButton pushBtn = myPanel.AddItem(new PushButtonData(“Test”,
“Test”,
addInPath,
typeof(TestCommand).FullName))
as PushButton;
pushBtn.ToolTip = “TestCommand”;
pushBtn.LargeImage = GetEmbeddedImage(myAssembly, “Test.png”);
pushBtn.Image = GetEmbeddedImage(myAssembly, “Test_small.png”);
return Result.Succeeded;
}
private ImageSource GetEmbeddedImage(System.Reflection.Assembly app, string imageName)
{
System.IO.Stream file = app.GetManifestResourceStream(“myRevitApp.” + imageName);
PngBitmapDecoder bd = new PngBitmapDecoder(file, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
return bd.Frames[0];
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
}
Notes:
Please make sure add “PresentationCore” and “WindowsBase” .NET references to the project, and add namespace “System.Windows.Media;” and “System.Windows.Media.Imaging;”.
In addition, make sure add the image resources to your project and set their property “Build Action” as “Embedded Resource”.
Comments
Leave a comment