Posted on 23. March 2009

Images and the Ribbon in Revit 2010

Compared with the path only option previously available, passing the Button data an ImageSource is a vast improved in the Revit 2010 ribbon implementation. However, manipulating Image objects in .NET3.5 is quite different to Images in .NET2.0.

The Ribbon sample in the SDK shows you how to load an image from a path. I prefer to embed the images in the assembly . Unfortunately how you do this isn’t so obvious in .NET3.5. Here’s one method showing you how to do it.

I generally put the images in a subdirectory of the project rather than in the root project directory:

 DirectImages

You must set the build action property of the images to embedded resource:

 EmRes

This is the method that gets the ImageSource from the embedded stream for each embedded Image:

/// <summary>
/// Gets the ImageSource from the embedded stream.
/// </summary>
/// <param name="ImageFullPath">The image full path.</param>
/// <returns>ImageSource</returns>
private ImageSource GetImageStream(string ImageFullPath)
{
    System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream(ImageFullPath);
    if (stream != null)
    {
        var bitmapDecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        return bitmapDecoder.Frames[0];
    }
    return null;
}

You can use gif’s bmp’s or jpegs you just need to change the decoder to the appropriate decoder for the image type. To use the above solution in your code you must specify the full name of the resource including the namespace. So for the above example it would look something like this:

ButtonData recordButton = new PushButtonData("One", "One", "C:\R2010\CommandOne.dll", "Redbolts.CommandOne.Command");
recordButton.LargeImage = GetImageStream("Redbolts.CommandOne.Images.One24.png");
recordButton.Image = GetImageStream("Redbolts.CommandOne.One16.png");

Make sure to add images for both the Image and LargeImage properties of the Button.

In conclusion, the move to an ImageSource is a small but very useful change in the Revit 2010 API.

Comments are closed