How to display image of ribbon button C#

How to display image of ribbon button C#

KinAkira
Advocate Advocate
4,353 Views
5 Replies
Message 1 of 6

How to display image of ribbon button C#

KinAkira
Advocate
Advocate

Hi guys,

I searched many pages and tried them. But I have some troubles with display image for a new ribbon button.

  • I dont know where I can put my image to it work.
  • I wonder After build, if others PC load this .dll file then this image will be showed ?

Anyone can help!

I have some following codes:

 

BitmapImage getBitmap(string fileName)
        {
            BitmapImage bmp = new BitmapImage();            
            bmp.BeginInit();
            bmp.UriSource = new Uri(string.Format("pack://application:,,,/{0};component/{1}",
              Assembly.GetExecutingAssembly().GetName().Name, fileName));

            bmp.EndInit();
            return bmp;
        }
private void addPanel2(RibbonTab ribTab)
        {
            //create the panel source
            var ribPanelSource = new RibbonPanelSource();
            ribPanelSource.Title = "My Tool";

            //create the panel
            var ribPanel = new RibbonPanel();
            ribPanel.Source = ribPanelSource;
            ribTab.Panels.Add(ribPanel);

            //create button1
            var button1 = new RibbonButton();
            button1.Text = "Button1";
            button1.ShowText = true;
            button1.ShowImage = true;
            button1.Image = getBitmap("button1.png");

            //pay attention to the SPACE after the command name
            button1.CommandParameter = "PlineChecked ";
            button1.CommandHandler = new BtCommandHandler();

            ribPanelSource.Items.Add(button1);
            /*************/
            //Add separate between 2 button
            RibbonSeparator rbseparator = new RibbonSeparator();
            rbseparator.SeparatorStyle = RibbonSeparatorStyle.Line;
            ribPanelSource.Items.Add(rbseparator);
            /*************/
            //create button2
            var button2 = new RibbonButton();
            button2.Text = "Button2";
            button2.ShowText = true;
            //pay attention to the SPACE after the command name
            button2.CommandParameter = "LineChecked ";
            button2.CommandHandler = new BtCommandHandler();

            ribPanelSource.Items.Add(button2);

        }

 

 

0 Likes
Accepted solutions (2)
4,354 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor
Accepted solution

Since you are to create custom ribbon items by code, I'd think compile (embed) the images into your DLL (if you do not have large number of them), or into a separate resource DLL, would be more desirable. You simply add the images to your project's resources. This way, you would just hand the DLL (or DLLs) of yours to the users, no worries about where they could place the image files.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 6

KinAkira
Advocate
Advocate

@norman.yuan wrote:

Since you are to create custom ribbon items by code, I'd think compile (embed) the images into your DLL (if you do not have large number of them), or into a separate resource DLL, would be more desirable. You simply add the images to your project's resources. This way, you would just hand the DLL (or DLLs) of yours to the users, no worries about where they could place the image files.


Thank for your advise, @norman.yuan . I'd tried put image (which exported from icon CUI support 32x32 pixed) to my source and run again my project with above codes, Image of ribbon button were not showed. I think something wrong in my code. But cant find them. 😩

 

kinakira2013_0-1621819184355.png

kinakira2013_1-1621819250311.png       kinakira2013_2-1621819316763.png

 

 

 

0 Likes
Message 4 of 6

Jeff_M
Consultant
Consultant
Accepted solution

@KinAkira you need to add a Resources file to your project:

2021-05-24_12-49-05.png

 

Then add the image to the Resources1.resx:

2021-05-24_12-50-40.png

Then you will be able to reference it like so:

Bitton1.Image = Reference1.line_large.ToBitmapImage();

The ToBitMapImage() is an extension method:

 

    public static class BitmapExtensions
    {
        public static BitmapImage ToBitmapImage(this Bitmap image)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();
            return bi;
        }
    }

 

Jeff_M, also a frequent Swamper
EESignature
Message 5 of 6

KinAkira
Advocate
Advocate

So many thanks with support's @Jeff_M and @norman.yuan . My troubles'd been resolved 🙂

0 Likes
Message 6 of 6

Ta7a
Enthusiast
Enthusiast
Hello Jeff,

what is Reference1 that you mention here : Bitton1.Image = Reference1.line_large.ToBitmapImage();
0 Likes