Ðаn
Ðаn

Reputation: 10875

Convert an Icon to IPicture in .NET 4.0?

One of the standard and (somewhat) supported answers was to use Support.IconToIPicture from the Microsoft.VisualBasic.Compatibility assembly. However, in .NET 4.0, "This API is now obsolete".

Yes, there are various solutions out there, but I would think that if Microsoft was obsoleting this method, there would be another "supported" alternative.

Upvotes: 3

Views: 822

Answers (1)

Kerido
Kerido

Reputation: 2940

I believe, this API is obsolete, because IPicture is an OLE interface. By working with it, you dive into the unmanaged world which Microsoft doesn't want you to. You could reproduce the behavior by calling IntPtr aHIcon = Icon.GetHicnFromIcon(yourIcon);

Then you need to pass this handle to OleCreatePictureIndirect (the code is in C++):

PICTDESC aDesc;
aDesc.cbSizeofstruct = sizeof PICTDESC;
aDesc.picType = PICTYPE_ICON;
aDesc.icon.icon = aHIcon;

IPicture * aPict = 0;
OleCreatePictureIndirect(&aDesc, __uuidof(IPicture), TRUE, (void **) &aPict);

The only difficulty -- you will need to provide a COM interop code for wrapping the C++ structure and function call.

Upvotes: 2

Related Questions