Images and the Ribbon in Revit 2010

by Guy Robinson 23. March 2009 12:45

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

About the Author

A .NET software Developer providing custom applications and commands for architecture firms exclusively working with Autodesk Revit and integration with any associated applications. All from a little place north of Whitianga, New Zealand.

Page List

Disclaimer

I'm self employed so the opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway☺

© Copyright2008

Creative Commons License
Blog content is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

With the following exception. All code snippets, application and libraries are licensed under a a Apache License Version 2.0

Autodesk Revit®

Autodesk: Revit is a product that is wholly owned by Autodesk. Any reference to Revit,Revit API, Revit Architecture, Revit MEP or Revit Structure on this site is made acknowledging this ownership. Refer to Autodesk's own web site and product pages for specific trademark and copyright information. Autodesk represents a great many products and every attempt will be made to respect their ownership whenever one of these other products is mentioned on this site.