Welcome to X4U
Minimize 

Hello visitor.

This is the personal homepage of Helmut Obertanner.
I'm a computer freak and electronic enthusiast and I live in Munich in Germany - the hometown of the famous Oktoberfest.
Why the name X4U? In my case it's a synonym for everything is possible.
I'm an experienced full time C# developer and doing so since .Net Framework 1.0.
As technical enthusiast I started Office development using .Net technologies.
As I searched answers and found help in the Internet I decided to share my knowledge in newsgroups and forums too. For participation, helping other developers, writing how-to's and articles and doing consulting and trainings I received the Microsoft MVP Award for the 3rd time.

As Outlook C# specialist I founded the website Outlooksharp.de
Beside software development, I'm building electronic devices for customers.
Prototypes with SMD PCBs and complete electronic devices with USB connections are my typical portfolio.
You got an Idea and have no plan how to put it to work - give me a call and I tell you if I can help.
A complete solution starts from defining the requirements, design the schematic and PCB, build a functional prototype, program the firmware, program the windows software, design and build a housing prototype.
Usually I work with very cheap PIC 18F Microcontrollers, these multifunctional devices are programmed in C language. 

I'm located in Germany with satisfied customers around the globe.
Enjoy the content of my site and feel free to give me your feedback.
You're welcome.
X4U Blog
 
Jun8

Written by:Helmut Obertanner
6/8/2010 9:56 PM 

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.

 

 

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment  Cancel 
X4U Blog
 
Home  |  Hardware  |  Software  |  Services  |  Resources  |  Impressum