What is the easiest way to load images to controls like a <button> or a <togglebutton> into a Ribbon.
First you start by import an Image as Resource into your solution.

You can do it by opening the properties of your solution, select the "Resources"-tab and import an existing Image file.
In the example above you can see the Resources Property Page of Visual Studio2010.
Important: After importing an Image as Resource have a look at the name. It's not the full filename – after importing it, it's the filename without the extension.
So, when importing a file "questionred.png" it's listed as "questionred".
How will you access this Images in your Solution?
In the Ribbon.XML normally you define the getImage="Getimage_Method" attribute:
<buttonid="buttonMoveToNirwana"getImage ="buttonMoveToNirwana_GetImage" />
In the code file for your Ribbon you need to define the Method to return an stdole.IPictureDisp interface.
publicIPictureDisp buttonMoveToNirwana_GetImage(IRibbonControl control) {
returnnull;
}
All you need to do is to convert the Image from Resource to an IPictureDisp interface.
Accessing the Image is easy, the code is already generated for you – you will access it this way:
Properties.Resources.questionred
To convert it to IPictureDisp you need a small helper class that does the trick for you.
In this sample the class is called ImageHelper:
using System.Windows.Forms;
using System.Drawing;
using stdole;
namespace RibbonExplorer {
internalclassImageHelper : AxHost {
private ImageHelper()
: base(null) {
}
publicstaticIPictureDisp Convert(System.Drawing.Image image) {
return (IPictureDisp)AxHost.GetIPictureDispFromPicture(image);
}
}
}
Getting the Image for the Ribbon Control is as easy as this:
publicIPictureDisp buttonMoveToNirwana_GetImage(IRibbonControl control) {
returnImageHelper.Convert(Properties.Resources.questionred);
}
The Drawback is that the Image resources are strongly typed.
To make it a bit more flexible we will extend the ImageConverter class with this Method:
publicstaticIPictureDisp GetImage(string resourceName) {
return Convert((Image)Properties.Resources.ResourceManager.GetObject(resourceName));
}
We will pass the resource name to the method and return the Image directly as IPictureDisp.
Then we can implement the method for retrieving the Image for the button this way:
publicIPictureDisp buttonMoveToNirwana_GetImage(IRibbonControl control) {
returnImageHelper.GetImage("questionred");
}
This is easy and flexible.
Greets – Helmut.