Embedded Ribbon Panel Image

Embedded Ribbon Panel Image

Mark.Ackerley
Advocate Advocate
1,563 Views
7 Replies
Message 1 of 8

Embedded Ribbon Panel Image

Mark.Ackerley
Advocate
Advocate

Hi all,

 

Really sorry, but I can't get this to work.  I am sure it's really basic.  I'm just starting to learn C#

 

Following on from here...

 

https://forums.autodesk.com/t5/revit-api-forum/ribbon-panel-button-image-from-embedded-resource/m-p/...

 

I have an embedded resource .png which I cannot get to link into my  project correctly. 

 

This is obviously wrong...

PNGtoImageSource("HTA_Ribbon.totalLength.png");

image.png

Any assistance greatfully received.

 

Apologies again for such a basic question.

 

Thanks,

 

Mark

 

 

 

 image.png

 

Code below

 

using System;
using System.Reflection;
using System.IO;

using Autodesk.Revit.UI;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace HTA_Ribbon
{
    class App : IExternalApplication
    {
        private ImageSource PNGtoImageSource(string embeddedPath)
        {
            Stream myStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedPath);
            PngBitmapDecoder decoder = new PngBitmapDecoder(myStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            return decoder.Frames[0];
        }
            // define a method that will create our tab and button
            static void AddRibbonPanel(UIControlledApplication application)
        {
            // Create a custom ribbon tab
            String tabName = "HTA";
            application.CreateRibbonTab(tabName);

            // Add a new ribbon panel
            RibbonPanel ribbonPanel = application.CreateRibbonPanel(tabName, "Tools");

            // Get dll assembly path
            string thisAssemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

            // create push button for CurveTotalLength
            PushButtonData b1Data = new PushButtonData(
                "cmdCurveTotalLength",
                "Total" + System.Environment.NewLine + "  Length  ",
                thisAssemblyPath,
                "Curve_Total_Length.CurveTotalLength");

            //add push button to ribbon
            PushButton pb1 = ribbonPanel.AddItem(b1Data) as PushButton;
            pb1.ToolTip = "Select Multiple Lines to Obtain Total Length";

            // create embedded Large Image 
            BitmapImage pbImage = PNGtoImageSource("HTA_Ribbon.totalLength.png");
            pb1.LargeImage = pbImage;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            // do nothing
            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            // call our method that will load up our toolbar
            AddRibbonPanel(application);
            return Result.Succeeded;
        }
    }
}
0 Likes
Accepted solutions (1)
1,564 Views
7 Replies
Replies (7)
Message 2 of 8

BenoitE&A
Collaborator
Collaborator

Have you tried the internet ?

https://stackoverflow.com/questions/6503424/how-to-programmatically-set-the-image-source/6503445

I think it is the syntax we used, and it workks...


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 3 of 8

Mark.Ackerley
Advocate
Advocate

Thank you for the reply 🙂

 

Yes the tutorial I was following used a uri but it was failing in 2019, the message said it failed to load 😞

 

http://archi-lab.net/create-your-own-tab-and-buttons-in-revit/

 

In the previously referenced post, Jeremy said embedded was better and I tried using the code supplied, but it isn’t working.

 

I did lots of googling but can’t get it to work.

 

All suggestions welcomed 😄

 

Mark

0 Likes
Message 4 of 8

BenoitE&A
Collaborator
Collaborator

Hey,

We sell a ribbon and we definitely use an URI like this

BitmapImage BT_img = new BitmapImage(new Uri(iconPath + "LogIn.bmp"));

And it works with 2019.

Benoit


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 5 of 8

BenoitE&A
Collaborator
Collaborator

The bottom of it is : you say you have a problem with a function (ImageToPNG() or something like this) and you don't provide the code for this. Since this function is not natural C# we don't know what it's for, and we can't help you !

But anyway the URI way works, so you have at least one solution...


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes
Message 6 of 8

Mark.Ackerley
Advocate
Advocate

 

Thanks for the response.

 

As I understand it, the code for the function is at the top? 

    {
        private ImageSource PNGtoImageSource(string embeddedPath)
        {
            Stream myStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedPath);
            PngBitmapDecoder decoder = new PngBitmapDecoder(myStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            return decoder.Frames[0];
        }

I didn’t post the code for the linked file because it isn’t the preferred solution. But I can recreate it and post it when I’m back in the office.

 

I would prefer to embed the image if possible.

 

Thanks,

 

Mark

0 Likes
Message 7 of 8

BenoitE&A
Collaborator
Collaborator

Hi again,

Sorry had not seen your method, I don't use it but it looks ok.

Then I don't know what actually does not work. Here are some ideas :

- check first that the path and the name of the image are correct

- then there is the stuff about the dpi of the image, which is an old problem that Autodesk has not fixed : 

https://thebuildingcoder.typepad.com/blog/2010/09/ribbon-icon-resolution.html

Benoit


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
Message 8 of 8

Mark.Ackerley
Advocate
Advocate
Accepted solution

Thanks for getting back to me.

 

It seems my use of "_" in the NameSpace was upsetting it.

 

I presume it's how the URI works... I guess it's conversion of symbols to a path was being messed up.

 

It also failed with "-" in the image name.

 

All is well 😄

 

Thanks again,

 

Mark

0 Likes