ImageMso to CommandBarButton Picture

Add-in Express™ Support Service
That's what is more important than anything else

ImageMso to CommandBarButton Picture
 
James Irish




Posts: 6
Joined: 2010-06-17
Hi Guys,

Adx2010 - Visual Studio 2010 - Office 2007 + 2010

I would like to ensure consistency between an Outlook 2010 Explorer Ribbon and it's equivalent Outlook 2007 Menu bar -- I consider the Ribbon to be primary and the command bar secondary.

I've got code that correctly lifts the ImageMso property of a ribbon button and gets the IPicutreDisp and System.Drawing.Image, as envidenced by the fact that the code below creates a BMP on my C drive.

Using that Image I am attempting to add it to an ImageList and then update the command bar button to display it. Currently it is not working and I'm not getting any errors. Could you possibly steer me in the right direction?

private void AddinModule_AddinStartupComplete(object sender, EventArgs e)
{
    OET10.Container.StructureMap.BootstrapStructureMap(Assembly.GetExecutingAssembly());

    var img = GetImageFromImageMso("AdpPrimaryKey", 16, 16);
    img.Save(@"C:AdpPrimaryKey.bmp");

    CommandBarImageList.Images.Add(img);
    CommandBarButtonHistory.ImageList = CommandBarImageList;
    CommandBarButtonHistory.Image = 0;
    CommandBarButtonHistory.UpdateControlObj(this);

    CommandBarImageList.Images.Add(GetImageFromImageMso(RibbonButtonAboutOET10.ImageMso, 16, 16));
    CommandBarButtonAbout.ImageList = CommandBarImageList;
    CommandBarButtonAbout.Image = 1;
    CommandBarButtonAbout.UpdateControlObj(this);
}
private System.Drawing.Image GetImageFromImageMso(string imageMso, int width, int height)
{
    var iPicDisp = OutlookApp.ActiveExplorer().CommandBars.GetImageMso(imageMso, width, height);
    return AxHostConverter.PictureDispToImage(iPicDisp);
}


Many Thanks,

James
Posted 31 Aug, 2012 07:18:35 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi James,

Please note that you don't release underlying COM objects properly in the code. You can read more about this in the When to release COM objects in Office add-ins developed in .NET article on our technical blog.

Did you set the Style property of your command bar buttons to the ADXMsoButtonStyle.adxMsoButtonIconAndCaption value? Anyway, I have just checked it out in one of my virtual PCs with Office 2003 installed (I used Excel 2003 in my tests):

adxCommandBarButton1.Image = 1;


The single line of code works like a charm. Could you please confirm?

If it doesn't work, please prepare a newly created add-in project which can reproduce the issue and then send it to the support e-mail address (see readme.txt).
.NET, Extended MAPI, Windows Phone & RT
Posted 31 Aug, 2012 11:28:32 Top
James Irish




Posts: 6
Joined: 2010-06-17
Hi Eugene,

Spot on with the style - that was the problem! I've updated the code above to release the explorer and command bars references. Don't worry we fully intend to spend some time checking the references and running a memory profiler over our final solution, but that's some way off at this point!

As an aisde, do you know of any way of improving the image quality when going between IPictureDisp and assigning the image to AdxCommandBarButton? Will assigning the IPictureDisp directly to the 'Picture' property of the underlying COM CommandBar help at all?

I'm using the following to get the Image..

public class AxHostConverter : AxHost
{
    private AxHostConverter() : base("") { }
        
    public static stdole.IPictureDisp ImageToPictureDisp(Image image)
    {
        return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);
    }
    public static Image PictureDispToImage(stdole.IPictureDisp pictureDisp)
    {
        return GetPictureFromIPicture(pictureDisp);
    }
}


Any suggestions?

Many thanks again,

James
Posted 31 Aug, 2012 11:48:33 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi James,

Of course, assigning the Image property of the CommandBarButton class directly (without converting it to the Image class) will improve the add-in performance and prevent an image from loosing quality.
.NET, Extended MAPI, Windows Phone & RT
Posted 31 Aug, 2012 14:57:27 Top