Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

In an ArcMap 10 add-in, I am attempting to create a toggle button of sorts whose image changes based on the button's state.

This answer describes how to set an add-in button's image programmatically and I am using the same technique. The problem is that this method does not appear to support full alpha channel transparency, only single-color transparency, so those fancy PNG icons they added at ArcGIS 10 don't look quite right.

The button's image is set initially by the add-in framework (using a 32-bit PNG file with alpha channel) -- the image looks as you would expect with proper partial transparency.

To create the image toggling behavior, in the button's OnClick method, I check the state of a boolean variable and set the button's image (using the technique in the above-mentioned answer) to one of two bitmaps stored as resources in the add-in assembly. This all works fine except that the button's image is given a gray background instead of a transparent background.

I have searched around a bit and come to the conclusion that ESRI's GetIPictureDispFromBitmap method converts from a GDI+ bitmap to a GDI bitmap and in doing so loses the alpha channel of the original bitmap.

I have found a solution to this issue and will post it as an answer shortly.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

To maintain partial alpha channel transparency, an IPictureDisp object must be created using some unmanaged code, described in this post on Ryan Gregg's blog.

Copy the C# (mirror) or VB.NET (mirror) code to a new code file and add it to your project. Then, instead of the technique in the previously-mentioned answer, set ICommandItem.FaceID to the return value of PictureDispConverter.ToIPictureDisp, passing in your original bitmap.

For example:

UID uid = new UIDClass();
uid.Value = ThisAddIn.IDs.MyButton;
ICommandItem cmdItem = ArcMap.Application.Document.CommandBars.Find(uid, true, true);
cmdItem.FaceID = PictureDispConverter.ToIPictureDisp(Resources.MyButtonEnabledState);

The partially-transparent areas of the original bitmap should be maintained.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.