Embedded bitmap for button icon RRS feed

  • General discussion

  • Hi,

    I wanted to use an embedded bitmap for an icon on a custom toolbar but the only way I found was to basically store the bitmap in to the clipboard, and then paste it on to the button, and I found this unreliable, as sometimes I just ended up with a blank button, and on certain PC's it never seemed to work (something broke with the clipboard? third party software interferring with clipboard? I'm not sure...). So I opted for the solution of using a seperate resource DLL....

    Recently I needed to be able to change a button image after it had been setup, and couldn't find a way of doing this (I couldn't find a method that lets you specify a resource ID for a button image) so I decided to look further in to using images from an embedded bitmap, and finally (after much experimentation and searching) I have found a solution, I share it here in case it is of interest to anyone else (sorry if it's already been documented elsewhere - I struggled to find it if it is...):

    Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.Stream resourceStream = executingAssembly.GetManifestResourceStream("NAMESPACE.FILE.bmp");
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(resourceStream);

    myCommandBarButton.Picture = (stdole.StdPicture)Microsoft.VisualBasic.Compatibility.VB6.Support.ImageToIPicture(bitmap);
    myCommandBarButton.Mask = (stdole.StdPicture)Microsoft.VisualBasic.Compatibility.VB6.Support.ImageToIPicture(bitmap);

    Note, the second line is only needed if you are using a mask to specify transparent/opaque areas (black = opaque, white = transparent).

    Hope this helps someone!

    Friday, September 21, 2007 10:21 AM