Does anybody know how to convert a IPictureDisp to an Outlook picture? RRS feed

  • Question

  • Hi All,

    I have created a replace-all form region and I have a command button that I am trying to set an image for for a button control. I loaded the ICON from a resource and created an IPictureDisp. I am trying to call _OlkCommandButton::put_Picture method with the picture disp, which is  not working. Obviously I have to creat a Picture object and I have been unsuccessful. Here is a little snippet of code.

    CComPtr < Outlook::_OlkCommandButton> m_spButton;
    std::vector< CComPtr<IPictureDisp> > m_image;
    CComPtr<IPictureDisp> spPictureDisp;
    GetPictureFromIcon32x32Resource(IDI_LOGO, &spPictureDisp);
    m_spButton->put_Picture(spPictureDisp); // not working, and is expecting a Picture*

    Does anybody know an efficient way to convert? I have tried several methods and all have been unsuccessful.



    Tuesday, April 3, 2018 9:01 PM

All replies

  • As near as I can tell an IPictureDisp and PIcture are the same thing.  The IID for IPictureDisp is 7BF80981-BF32-101A-8BBB-00AA00300CAB and according to the Ole Automation Ver2.0 type library (stdole2.tlb) viewed with oleview.exe a Picture dispinterface has the same IID.

    You should be able to simply use a cast to make the compiler happy.

    Tuesday, April 3, 2018 10:47 PM
  • Hi,

    Thanks for the reply -much appreciated. Yep, that's exactly what I thought.

    I typecasted and finally got an S_OK back. The image is still not showing, but perhaps I have something else going on.

    Wednesday, April 4, 2018 12:29 AM
  • Well, the next question is then how the GetPictureFromIcon32x32Resource function is obtaining the IPictureDisp interface?
    Wednesday, April 4, 2018 12:55 AM
  • Yes, it is a valid interface. I load images in my ribbon using the same function.

    But I think I figured out what is going on. I opened the form region up (in Outlook developer), and tried directly including the ICON and it fails. I converted the image to a bitmap and the image loads in the form. So there might be an issue using ICO images within a form region. I am going to do some more experiments and I will post results tomorrow.

    Thanks for your help. Its nice to have another developers input

    Wednesday, April 4, 2018 1:02 AM

    Error when I try to include an ICO directly

    • Marked as answer by Thomas Lee3 Wednesday, April 4, 2018 12:00 PM
    • Unmarked as answer by Thomas Lee3 Wednesday, April 4, 2018 12:52 PM
    Wednesday, April 4, 2018 1:12 AM
  • Have you tried using OleCreatePictureIndirect function to obtain an IPictureDisp (Piicture) from an HICON?

    • Edited by RLWA32 Wednesday, April 4, 2018 1:59 AM
    • Proposed as answer by Terry Xu - MSFT Wednesday, April 4, 2018 6:16 AM
    Wednesday, April 4, 2018 1:58 AM

    Error when I try to include an ICO directly

    I don't understand how this post can be the answer to your question.  Am I missing something?
    Wednesday, April 4, 2018 12:13 PM
  • Oops, sorry about that, I clicked by accident and meant to click reply.

    I will try OleCreatePictureIndirect function later today. 

    Thanks for your help on this

    Wednesday, April 4, 2018 12:59 PM
  • I have tried several ways to set the image using the put_Picture function and all fail. Either the button becomes destorted, and the text disapears. Or if I try to set the picture to use an icon (no matter which way I construct the picture object), and error is produced. So I think something is not quite correct with the button interface as it relates to a form region.

    As a quick fix, I decided to create two controls and hide the one without the proper image. This is a quick fix until I have time to come back and really troubleshoot this issue.

    If I had to bet, I would say that it is something to do with casting to the picture interface - my original issue.

    Wednesday, April 4, 2018 9:53 PM
  • An OlkCommandButton does not display both a picture and text at the same time. It is documented behavior that if a picture is used the text is ignored.  See OlkCommandButton.Picture Property

    The same docs also  refer to clipping a picture to fit.

    Wednesday, April 4, 2018 10:34 PM
  • So then what is the proper button control that the form region is using? Because I am displaying both an image and text in the form and it displays properly when I load the image in the advanced properties. So something is not correct somewhere

    Wednesday, April 4, 2018 11:15 PM
  • As you noticed, the OlkCommandButton doesn't seem to like objects created from icons.  I loaded a sample native C++ add-in that creats a form region and regardless of how I created the Picture interface the OlkCommandButton method always returned E_INVALIDARG.

    So then I used OleCreatePictureIndirect to create a Picture * from a bitmap.  The OlkCommandButton accepted the Picture * object created by OleCreatePictureIndirect from a bitmap and displayed both the image AND the button caption.  Don't ask me why the documentation says it behaves otherwise.

    Following is the code snippet I used to insert the image on the button -

    HBITMAP hbmp;
    PICTDESC pd = { sizeof pd };
    hbmp = (HBITMAP) LoadImage(_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 32, 32, 0);
    if (hbmp)
    	Picture *pPic = NULL;
    	pd.picType = PICTYPE_BITMAP;
    	pd.bmp.hbitmap = hbmp;
    	hr = OleCreatePictureIndirect(&pd, IID_IPicture, TRUE, (LPVOID *)&pPic);
    	if (SUCCEEDED(hr))
    		hr = pOlk->put_Picture(pPic);
    		hr = pOlk->put_PictureAlignment(Outlook::olPictureAlignmentTop);

    Thursday, April 5, 2018 2:08 PM
  • Thank you very much for verifying that. I have code simular to yours that is working. I was in the process of creating a new VM and installing a different version  of office (2013) to see if I experienced the same issues.

    Again, thank you for going above and beyond.

    Thursday, April 5, 2018 2:38 PM
  • Happy to help out.

    It is also interesting to note that the IPictureDisp/Picture objects from an HICON that were rejected by the OlkCommandButton were happily accepted and displayed on the ribbon for an Inspector.  Your guess is as good as mine, but at least we know that the problem is not related to casting between the two types with identical IIDs.

    BTW, I did my tests on Outlook 2013. :)

    Thursday, April 5, 2018 2:55 PM
  • Yeah, that was the confusing part to me too. Why were the ribbon buttons working?

    Well, at least we got to the bottom of the issue.

    Glad that you tested 2013, because the add-in I am developing has to support 2013


    Thursday, April 5, 2018 3:00 PM