3 Replies Latest reply on Mar 16, 2012 3:59 PM by mhamel

    How to use a .ico or .png file as an icon in an add-in toolbar?


      Hi everyone,


      I am changing the appearance of an Add-in Toolbar but I was able to use .bmp files as icons.
      The problem is that .bmp images are of very poor quality and all of them are kind of blurred while the same images as .ico or .png look very nice and sharp.


      By now I am using LoadFace method that does not allow to use .ico and .png.


      I hope it'd be possible!

      Thanks in advance.


      Fábio Reis

        • Re: How to use a .ico or .png file as an icon in an add-in toolbar?

          @Fabio: It is possible but you will need to convert your .png or .ico file into a .bmp first as the LoadFace method can only handle bitmap files. I have added a code snippet to show you how you can convert your image directly in your code.

          private void SetCommandBarButton()
               PBCommandBarButton MyButton;
               PBCommandBar StdToolBar;
               string path = "C:\\OriginalPath\Myfile.png";
               string temppath = "C:\\temp\\ConversionFile.bmp";            
               System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(path);
               //Convert the *.png into a *.bmp file and save it temporarily to a folder.
               //Reference the PB File Toolbar if it is where you want to add a button.
               StdToolBar = m_theApp.CommandBars.Item("Standard Toolbar");
               //Show the toolbar if it was hidden.
               StdToolBar.Enabled = true;
               //Add the button.            
               MyButton = (PBCommandBarButton)StdToolBar.Controls.Add((Object)PBObjLib.pbControlType.pbControlButton, 1, null, null, false);
               //Set your caption, icon and style.
               MyButton.Caption = "MyButton";
               MyButton.Style = pbButtonStyle.pbButtonIcon;
               //Set the button visible.
               MyButton.Visible = true;



          Let me know if that helped you.

            • Re: How to use a .ico or .png file as an icon in an add-in toolbar?

              @Mathieu: First of all, thanks for your response. So, maybe I was not clear as I wanted to be. As I mentioned, I am aware that I can use .bmp files and that I was not able to use .png or .ico files with LoadFace method, since converted files (from .png to .bmp) have a poor quality output image. This lack of quality makes the .bmp files useless.


              Does it have any other way to put an icon (.ico or .png file) directly in and add-in toolbar?





                • Re: How to use a .ico or .png file as an icon in an add-in toolbar?

                  @Fabio: Two methods are allowed to put a picture on a button within PI ProcessBook: LoadFace and LoadFaceResource. The former one uses a bitmap type file (*.bmp, *.gif, etc.) the latter one uses a library (*.dll or *.exe) with Win32 embedded resources from which you specified the bitmap resource ID. In your case both won't let you use *.png file directly.


                  With the code snippet I shared with you, you can use a codec to perform a good conversion between the *.png file to a *.bmp one. This is a third argument (System.Drawing.Imaging.EncoderParameters) to pass to the Save method of the Bitmap class. Some codecs are free and can be easily found on the Internet.


                  Although, IMHO I think all this is not as worthful as using a good image processing software (such as Photoshop, Gimp which is open-source BTW, etc.) to convert your *.png file into a *.bmp one and handling the file in a conventional way.


                  As other guideline, the bitmap image you use for a custom PBCommandBarButton picture should be a 256-color device independent bitmap (DIB) no bigger than 16 x 16 pixels in size. You can use any graphics editor to create these bitmaps, provided that it can save images as a standard Windows bitmap (.bmp or .dib) file, but be sure not to use high-color images because these may appear distorted on some systems with lower resolution. Also, choose a color that is not likely to appear in your main images, such as magenta (RGB(255, 0, 255)), and use it to fill in those areas that you want to make transparent.


                  Hope this help!