Sunday, 14 October 2018

arcgis 10.0 - ArcObjects: Setting image of a command button to a partially transparent bitmap


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.



Answer




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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...