Freigeben über


Converting Bitmaps to IPictureDisp

If you're writing your Form Region solution in managed code, and you decide to provide the manifest information as a resource in the add-in, you'll need to return all of your icons for the form region in a format Outlook knows how to parse. Unfortunately, Outlook doesn't handle the managed code types System.Drawing.Bitmap and System.Drawing.Icon so you need to find some way to convert these types into something Outlook does understand. There have been a couple of examples of how to convert a Bitmap or Image to an IPictureDisp, but I haven't found anything on how to make this work for Icon and actually preserve the format as an Icon (without converting to Bitmap first).  Outlook is very picky about the type of IPictureDisp objects it expects.  If you are providing an icon to be used in a view or window, you need to provide an IPictureDisp with a subtype of Icon.  If you are providing an icon for the Ribbon, you should use a Bitmap subtype.

Thomas Quinn on the VSTO team came up with two extremely useful class files that make this super easy: one for C# developers and one for VB.NET developers. Just add the code file to your project, and then call the PictureDispConverter.ToIPictureDisp() and provide the Bitmap, Image, or Icon object and the method returns an IPictureDisp object you can send back to Outlook. The advantage to using this class is that it actually outputs different types of IPictureDisp based on the input format, so you don't need to convert an Icon to a Bitmap type first.

PictureDispConverter.cs
PictureDispConverter.vb

Comments

  • Anonymous
    December 01, 2006
    How about Microsoft.VisualBasic.Compatibility.VB6.Support.IconToIPicture(). There is also an undocumented AxHost.GetIPictureDispFromPicture()

  • Anonymous
    December 01, 2006
    Those should both work, with IconToIPicture providing the Icon format and GetIPictureDispFromPicture providing the bitmap format.  However, since the AxHost method isn't documented, and the other is a VB6 compatibility member, I would recomend using the classes TQ developed in place of those two calls.