Get the thumbnail from active inventor document using Inventor 2023 c# api

Get the thumbnail from active inventor document using Inventor 2023 c# api

sneha.sadaphal
Advocate Advocate
3,295 Views
9 Replies
Message 1 of 10

Get the thumbnail from active inventor document using Inventor 2023 c# api

sneha.sadaphal
Advocate
Advocate

hi,

I am facing issue while getting thumbnail from inventor document object. it is coming as null object in thumbnail.

here is sample code which i am trying to create for thumbnail


Inventor.AssemblyDocument document = InventorApp.ActiveDocument as AssemblyDocument;
PropertySet summaryInfo = document.PropertySets["Inventor Summary Information"];
Property thumbProp = summaryInfo["Thumbnail"];
stdole.IPictureDisp thumb = document.Thumbnail;

 

right now the thumb value is null as shown in below image 

snehasadaphal_0-1675402362165.png

 

Is there any work around to get the thumbnail from active document or from active view?

0 Likes
Accepted solutions (1)
3,296 Views
9 Replies
Replies (9)
Message 2 of 10

JelteDeJong
Mentor
Mentor

Getting the thumbnail image from the iProperties will only work on 32bit versions of Inventor according to this post.

Because all versions of Inventor are now 64bit I don't think this is a real option. In that post, you will also find other options to get the thumbnail. However, I doubt if they will work.

I think your best option is to create an image yourself using the camera function. (At least that is what I ended up using in one of my tools.) You can find more info about the camera function in these posts:

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 10

nmunro
Collaborator
Collaborator

An old post but an easy solution for 64-bit Inventor versions 2013 and newer.

 

Thumbnail extraction for add-ins - Autodesk Community - Inventor

 

 

        


https://c3mcad.com

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor

Hi guys.  I came across this bit of code at some point, that I only slightly modified, but seems to work OK for me, from an iLogic rule to export a documents thumbnail image out to a bmp file.  I think I picked most of this up from @Ralf_Krieg , but I do not recall the exact link to the source.

AddReference "System.Drawing"
AddReference "stdole"
AddReference "System.Windows.Forms"
Class ThisRule
	Sub Main
		Break
		Dim oDoc As Document = ThisDoc.Document
		Dim oThumb As stdole.IPictureDisp
		Do
			oThumb = oDoc.Thumbnail
		Loop While oThumb.Handle < 0
		Dim oAHConverter As New AxHostConverter
		Dim oImage As System.Drawing.Image = oAHConverter.GetImageFromIPictureDisp(oThumb)
		'this call must be created, but is never used
		Dim myCallback As New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
		'create a thumbnail before save - seems to generate a better quality
		oImage.GetThumbnailImage(180, 180, myCallback, IntPtr.Zero).Save("C:\Temp\Thumbnail To BMP test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
	End Sub

	Public Function ThumbnailCallback() As Boolean
		Return False
	End Function
End Class

Class AxHostConverter
	Inherits System.Windows.Forms.AxHost
	Public Sub New()
		'MyBase.New("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
		MyBase.New((New Guid).ToString)
	End Sub
	Public Function GetImageFromIPictureDisp(ByVal IPDisp As stdole.IPictureDisp) As System.Drawing.Image
		Return MyBase.GetPictureFromIPicture(IPDisp)
	End Function
End Class

   

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

WCrihfield
Mentor
Mentor

I also used to use this handy little VBA macro for exporting native Inventor command icons out to bitmap files, so that I could edit them for my own custom tool buttons.  The same technique I am using there could also be used for exporting the document thumbnail though.

Sub Get_CMD_Icon_As_BMP()
    Dim oDef As ButtonDefinition
    Set oDef = ThisApplication.CommandManager.ControlDefinitions.Item("iLogic.RegenAllRules")
    Dim oLargeIcon As IPictureDisp
    Set oLargeIcon = oDef.LargeIcon
    Dim oStandardIcon As IPictureDisp
    Set oStandardIcon = oDef.StandardIcon
    StdFunctions.SavePicture oLargeIcon, "C:\Temp\iLogic.RegenAllRules_LargeIcon.bmp"
    StdFunctions.SavePicture oStandardIcon, "C:\Temp\iLogic.RegenAllRules_StandardIcon.bmp"
End Sub

I believe I had to turn on the following reference, in order to get this to work.

OLE Automation

C:\Windows\System32\stdole2.tlb

WCrihfield_0-1675691861461.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 10

jjstr8
Collaborator
Collaborator
Accepted solution

In my case, I needed it as a BitmapImage so I made it into an Inventor extension.  I don't know if this is what you're looking for.  Also, I only needed a 300x300 image, so I hard-coded that in the GetThumbnailImage call.  Native size looks to always be 5644x5644.  Substitute the 300s with document.Thumbnail.Width and document.Thumbnail.Height, respectively, if that's what you want.

using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Media.Imaging;
using Inventor;
using System.IO;

    internal static class InventorExtensions
    {
        internal static BitmapImage GetThumbnailAsBitmapImage(this Document document)
        {
            BitmapImage docThumbnail;
            if (document == null) 
            {
                return null;
            }

            try
            {
                if (document.Thumbnail == null)
                {
                    return null;
                }
                Metafile thumbnailMetafile = new Metafile(new IntPtr(document.Thumbnail.Handle), new WmfPlaceableFileHeader());
                System.Drawing.Image.GetThumbnailImageAbort imageCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                Bitmap thumbnailBitmap = thumbnailMetafile.GetThumbnailImage(300, 300, imageCallBack, IntPtr.Zero) as Bitmap;
                docThumbnail = ConvertToBitmapImage(thumbnailBitmap);
                picMeta.Dispose();
                return docThumbnail;
            }
            catch (Exception ex) when (ex is System.Runtime.InteropServices.COMException || ex is ArgumentException)
            {
                return null;
            }
        }

        private static BitmapImage ConvertToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            if (bitmap == null)
            {
                return null;
            }

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            MemoryStream editStream = new MemoryStream();
            bitmap.Save(editStream, ImageFormat.Png);
            _ = editStream.Seek(0, SeekOrigin.Begin);
            bitmapImage.StreamSource = editStream;
            bitmapImage.EndInit();
            return bitmapImage;
        }

        private static bool ThumbnailCallback()
        {
            return false;
        }
    }

  

Message 7 of 10

sneha.sadaphal
Advocate
Advocate

hi, @jjstr8 thanks for the solution it is working as expected! 

0 Likes
Message 8 of 10

Raider_71
Collaborator
Collaborator

Hi any idea why this would not work? I have a standalone application here just to test but the Thumbnail property on a part document just throws an error when trying to access it. I have also tried the solution above and the same thing. Tried changing app from AnyCPU to x64 and that also did not help.

Any idea? Would this only work if an add-in (in-process) app?

 

2023-04-06_11-39-32.png

 

 

0 Likes
Message 9 of 10

jjstr8
Collaborator
Collaborator

Standalone applications must use Apprentice Server.

0 Likes
Message 10 of 10

kriegler
Contributor
Contributor

you're right, that only works in an addin

a good solution for me is to work with the Microsoft-WindowsAPICodePack-Shell 1.1.4 which you can get it from the NuGet.

   Public Function GetThumbnail(ByVal strFullFileName As String) As Image

        Dim imgTemp As Bitmap = Nothing
        Dim icoTemp As Icon = Nothing
        Using shFile As Microsoft.WindowsAPICodePack.Shell.ShellFile = Microsoft.WindowsAPICodePack.Shell.ShellFile.FromFilePath(strFullFileName)
            Try
                'imgTemp = shFile.Thumbnail.SmallBitmap
                imgTemp = shFile.Thumbnail.MediumBitmap
                'imgTemp = shFile.Thumbnail.LargeBitmap
                'imgTemp = shFile.Thumbnail.ExtraLargeBitmap
                'icoTemp = shFile.Thumbnail.Icon
                ' imgTemp.MakeTransparent()
            Catch ex As Microsoft.WindowsAPICodePack.Shell.ShellException

            Catch ex As Exception

            Finally
                If imgTemp Is Nothing Then
                    imgTemp = My.Resources.NoPicture96x96
                End If

            End Try
        End Using
        Return imgTemp

    End Function