Home > Enterprise >  issue adding images to buttons using visual studios 2022 C# for revit
issue adding images to buttons using visual studios 2022 C# for revit

Time:01-27

I'm running into an issue where everything works fine until I try to throw in the images. I'm trying to pull them in as resources from inside vs. Not sure what to do here being new to C#. Below is the code that's throwing me the errors. as well as the error message.
error message

#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.CSharp;
using System.Media;
using System.Reflection;
using System.Drawing.Imaging;
using System.IO.Packaging;
using System.Drawing; 
#endregion

namespace TpMechanical
{
    internal class App : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication a)
        {
            String tabname = "TpMechanical";
            String panelname = "Tools";
          
            Bitmap button1Image = (System.Drawing.Bitmap)TpMechanical.Properties.Resources.ResourceManager.GetObject("Untitled design.png");
            Bitmap button2Image = (System.Drawing.Bitmap)TpMechanical.Properties.Resources.ResourceManager.GetObject("Untitled design2.png");
            Bitmap button3Image = (System.Drawing.Bitmap)TpMechanical.Properties.Resources.ResourceManager.GetObject("Untitled design3.png");


            a.CreateRibbonTab(tabname);
            var Tools = a.CreateRibbonPanel(tabname, panelname);
            var button1 = new PushButtonData("TpButton1", "Button1", Assembly.GetExecutingAssembly().Location, "TpMechanical.command");
            button1.ToolTip = " This is a short description";
            button1.LongDescription = "This is a long description \n "  
                "this is the second line";
            var btn1 = Tools.AddItem(button1);

            var button2 = new PushButtonData("TpButton2", "Button2", Assembly.GetExecutingAssembly().Location, "TpMechanical.command2");
            button2.ToolTip = " This is a short description";
            button2.LongDescription = "This is a long description \n "  
                "this is the second line";
   
            var button3 = new PushButtonData("TpButton3", "Button3", Assembly.GetExecutingAssembly().Location, "TpMechanical.command3");
            button3.ToolTip = " This is a short description";
            button3.LongDescription = "This is a long description \n "  
                "this is the second line";
            
            Tools.AddStackedItems(button2, button3);
            

            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication a)
        {
            return Result.Succeeded;
        }
    }
}

CodePudding user response:

Importing an image from Resources using the standard bitmap library in c#

Bitmap b1Image = (System.Drawing.Bitmap)(WindowsFormsApp1.Properties.Resources.ResourceManager.GetObject("Name of the image"));

You will need to replace "WindowsFormsApp1" with the correct namespace.

As you are using System.Windows.Media.Imaging you will need:

BitmapImage b1Image = (System.Windows.Media.Imaging.BitmapImage)(WindowsFormsApp1.Properties.Resources.ResourceManager.GetObject("Name of the image"));

CodePudding user response:

Solved by @Luke Attard

Importing an image from Resources using the standard bitmap library in c#

Bitmap b1Image = (System.Drawing.Bitmap)(WindowsFormsApp1.Properties.Resources.ResourceManager.GetObject("Name of the image"));

You will need to replace "WindowsFormsApp1" with the correct namespace.

As you are using System.Windows.Media.Imaging you will need:

BitmapImage b1Image = (System.Windows.Media.Imaging.BitmapImage)(WindowsFormsApp1.Properties.Resources.ResourceManager.GetObject("Name of the image"));

  •  Tags:  
  • Related