Hi All,
I am trying to get an image out of the ContentTreeViewNode.Icon property (IPictureDisp) in an Inventor add-in to display in a picturebox. I have red all available postings about converting an IPictureDisp to Image, but the conversion keep on failing. Does anybody has a working sample?
Thanks!
Regards,
Pim
Solved! Go to Solution.
Solved by philippe.leefsma. Go to Solution.
Hi Pim,
Here is a VBA sample that saves a node resource to the disk:
Sub Test() Dim PrtDoc As PartDocument Set PrtDoc = ThisApplication.ActiveDocument Dim oFea As PartFeature Set oFea = PrtDoc.ComponentDefinition.features(1) Dim oFeatureNativeBrowserNodeDef As NativeBrowserNodeDefinition Set oFeatureNativeBrowserNodeDef = PrtDoc.BrowserPanes.GetNativeBrowserNodeDefinition(oFea) Dim pic1 As stdole.IPictureDisp Set pic1 = oFeatureNativeBrowserNodeDef.Icon Call stdole.SavePicture(pic1, "C:\temp\Extrude.bmp") End Sub
I hope it helps,
Philippe.
I don't see why, the VBA code runs in-process and you should be able to invoke the same functions from an add-in... I'll give a try and let you know.
Philippe.
The following code seems to work on my side. I'm attaching also the test project:
[DllImport( "oleaut32.dll", EntryPoint = "OleSavePictureFile", ExactSpelling = true, PreserveSig = false, SetLastError = true)] public static extern void OleSavePictureFile( stdole.IPictureDisp Picture, [MarshalAs(UnmanagedType.BStr)] string filename); void SavePic(stdole.IPictureDisp pic, string dest) { string filename = System.IO.Path.GetTempFileName(); OleSavePictureFile(pic, filename); System.Drawing.Image img = System.Drawing.Image.FromFile( filename, true); img.Save(dest, System.Drawing.Imaging.ImageFormat.Png); // exception temp file in use ... //System.IO.File.Delete(filename); } public void Activate(
Inventor.ApplicationAddInSite addInSiteObject,
bool firstTime) { m_inventorApplication = addInSiteObject.Application; PartDocument doc = m_inventorApplication.ActiveDocument as PartDocument; var feature = doc.ComponentDefinition.Features[1]; var def = doc.BrowserPanes.GetNativeBrowserNodeDefinition(feature); stdole.IPictureDisp pic = def.Icon; SavePic(pic, @"C:\Temp\icon.png"); }
Philippe.
Maybe the problem is the IPictureDisp internally is an Icon and not a Bitmap. Check the field:
pictureDisp.Type
you can have one of the following values
'Picture Types
Public Const PICTYPE_UNINITIALIZED As Short = -1
Public Const PICTYPE_NONE As Short = 0
Public Const PICTYPE_BITMAP As Short = 1
Public Const PICTYPE_METAFILE As Short = 2
Public Const PICTYPE_ICON As Short = 3
Public Const PICTYPE_ENHMETAFILE As Short = 4
Daniele
Hi Phillipe,
I tested your sample with the Content Center API and that seems to work. Strange thing though when I use a breakpoint on the line where I retrieve the Icon property, the code fails. If I just run in it works. I also tried again to convert the IPictureDisp to Image by using a couple of different functions that can be found on the internet, but also that still fails. Saving the icon as an temporary image file by the function you've provided and loading it again looks like the only working workflow.
Thanks for your help!