Add image icon to RibbonButton

Add image icon to RibbonButton

Anonymous
Not applicable
1,196 Views
3 Replies
Message 1 of 4

Add image icon to RibbonButton

Anonymous
Not applicable

I am unsure how to get an image icon into a button

 

I know the buttons have the following tags:

button.ShowText = true;

button.Image = ??

 

I am just unsure what to set the Image tag to and where to save the image

 

0 Likes
1,197 Views
3 Replies
Replies (3)
Message 2 of 4

Paulio
Advocate
Advocate

The ribbon button should be a BitmapImage and you can add the image as a resource to your project.

 

I've got a LoadImage function something like this:

Private Shared Function LoadImage(imageToLoad As String) As BitmapImage
    Dim image2 As BitmapImage
    Dim image As New BitmapImage()
    Try
        Dim bitmap As Bitmap = My.Resources.ResourceManager.GetObject(imageToLoad)
        Dim stream As New MemoryStream()
        bitmap.Save(stream, ImageFormat.Png)
        image.BeginInit()
        image.StreamSource = stream
        image.EndInit()
        image2 = image
    Catch generatedExceptionName As Exception
        image2 = Nothing
    End Try
    Return image2
End Function

Then you can just set your button image like this:

button.image=LoadImage(your-image-name)

I can't remember why I've got image and image2 (it was a very long time ago which is why it's in VB and I haven't looked at it for years) but it works.

HTH

 

Message 3 of 4

Anonymous
Not applicable

Thank you, I am looking for a solution in c#, hopefully someone can help

0 Likes
Message 4 of 4

gleeuwdrent
Advocate
Advocate

I embed the image files as Embedded Resource to my project, and then using this method to load them:

 

public static BitmapImage LoadImage(string imageName, int Height, int Width)
{
	BitmapImage image = new BitmapImage();

	image.BeginInit();
	image.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream(imageName);
	image.DecodePixelHeight = Height;
	image.DecodePixelWidth = Width;
	image.EndInit();
	return image;
}

 

And attaching the image to the button as follows:

button.Image = LoadImage($"{<yourProjectName>}.<yourSubfolderContainingTheImages>." + iconName + ".ico", 16, 16);
button.LargeImage = LoadImage($"{<yourProjectName>}.<yourSubfolderContainingTheImages>." + iconName + ".ico", 32, 32);