Save thumbnail as image file

ejaL9K8H
Advocate

Save thumbnail as image file

ejaL9K8H
Advocate
Advocate

Hi

 

How do i save the thumbnail with ilogic in a part-file?

I have written this ilogic code, but it is not working - Can somebody help me?

 

 

Imports System.Windows.Forms
Imports System.Drawing
AddReference "System.Drawing"
AddReference "stdole"
 
Sub Main
    ' Get the active document (must be a PartDocument)
    Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
    If oPartDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
        MsgBox("Please open a part document.")
        Exit Sub
    End If
 
    ' Get the thumbnail from iProperties
  Dim oThumbnail As stdole.IPictureDisp = oPartDoc.PropertySets.Item("Inventor Summary Information").Item("Thumbnail").Value
 
 
    ' Convert the IPictureDisp to a .NET Bitmap object using the converter class
    Dim oPicture As System.Drawing.Image = IPictureDispConverter.PictureDispToImage(oThumbnail)
 
    If oPicture IsNot Nothing Then
        ' Use the oPicture object, for example:
MsgBox("Thumbnail converted successfully!")
oPicture.Save("C:\TEMP\Thumbnail.jpg")
    Else
        MsgBox("Failed to convert thumbnail.")
    End If
End Sub
 
' Custom class that exposes the GetPictureFromIPictureDisp method from AxHost
Public NotInheritable Class IPictureDispConverter : Inherits AxHost
    Private Sub New()
        MyBase.New("")
    End Sub
 
    ' Function to convert IPictureDisp to Image
    Public Shared Function PictureDispToImage(oThumbnail As stdole.IPictureDisp) As Image
        Try
            ' Use the protected method from AxHost to convert IPictureDisp to Image
            Return GetPictureFromIPictureDisp(oThumbnail)
        Catch ex As Exception
            myparam = InputBox("Error converting: ", "Conversion Error", ex.Message)
            Return Nothing
        End Try
    End Function
End Class

 

 

 

0 Likes
Reply
Accepted solutions (1)
1,116 Views
14 Replies
Replies (14)

WCrihfield
Mentor
Mentor

Hi @ejaL9K8H.  I am not 100% sure if this is the only detail, but it looks to me like you have not created a 'New' instance of the 'IPictureDispConverter' Class, before trying to use the method defined within that Class.  So, you may need to use an extra line of code to declare a variable to represent an instance of that Class, then use the 'New' keyword when setting its value to that Class Type.  Then use that variable in that line that is trying to use the method within that Class.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Michael.Navara
Advisor
Advisor

I have been using different conversion method between Image and IPictureDisp for many years and it works well. Here is my sample.

Note: Converted image is not JPG but PNG.  

 

 

AddReference "stdole"
AddReference "System.Drawing"


Sub Main()
	' Get the active document (must be a PartDocument)
	Dim oPartDoc As PartDocument = ThisApplication.ActiveDocument
	If oPartDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		MsgBox("Please open a part document.")
		Exit Sub
	End If

	' Get the thumbnail from iProperties
	Dim oThumbnail As stdole.IPictureDisp = oPartDoc.Thumbnail
	
	' Convert the IPictureDisp to a .NET Bitmap object using the converter class
	'Dim oPicture As System.Drawing.Image = IPictureDispConverter.PictureDispToImage(oThumbnail)
	Dim oPicture As System.Drawing.Image = PictureDispToImage(oThumbnail)

	If oPicture IsNot Nothing Then
		' Use the oPicture object, for example:
		MsgBox("Thumbnail converted successfully!")
		oPicture.Save("C:\TEMP\Thumbnail.png")
	Else
		MsgBox("Failed to convert thumbnail.")
	End If
End Sub

Public Shared Function PictureDispToImage(pictureDisp As stdole.IPictureDisp) As System.Drawing.Image
	Dim image As System.Drawing.Image
	If pictureDisp IsNot Nothing Then
		If pictureDisp.Type = 1 Then
			Dim hpalette As IntPtr = New IntPtr(pictureDisp.hPal)
			image = image.FromHbitmap(New IntPtr(pictureDisp.Handle), hpalette)
		End If
		If pictureDisp.Type = 2 Then
			image = New System.Drawing.Imaging.Metafile(New IntPtr(pictureDisp.Handle), New System.Drawing.Imaging.WmfPlaceableFileHeader())
		End If
	End If
	Return image
End Function

 

I have 

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Michael.Navara.  I like that your example code does not appear to need to define a special Class to work with, but it produces the same problematic results as the other methods do...the image looks like it has been squished from top to bottom.  I have seen this process before on StackOverflow (Link), but I did not understand the IPictureDisp.Type property, so never really explored it any further.   I believe an extra step is needed in the middle, just before saving the image to disk, where it sets the size / dimensions of the image to be at least the same 'ratio', if not the same size, as the original.  So, I copied your code, modified a few things, such as eliminating document type check (works good on all document types for me), and changing a few variable names, then inserted an extra line to set the size of the image, just before it gets saved to file.  It is working OK so far.

AddReference "stdole"
AddReference "System.Drawing"
Imports System.Drawing
Imports System.Drawing.Imaging
Sub Main
	Dim oDoc As Document = ThisDoc.Document
	Dim oThumbnail As stdole.IPictureDisp = oDoc.Thumbnail
	Dim oPicture As System.Drawing.Image = PictureDispToImage(oThumbnail)
	If oPicture IsNot Nothing Then
		oPicture = New Bitmap(oPicture, New Size(oThumbnail.Width, oThumbnail.Height))
		Dim sNewFile As String = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".png")
		oPicture.Save(sNewFile, System.Drawing.Imaging.ImageFormat.Png)
		Logger.Info("Save Document Thumbnail To Image File Worked!")
	Else
		Logger.Debug("Save Document Thumbnail To Image File Failed!")
	End If
End Sub

Public Shared Function PictureDispToImage(pictureDisp As stdole.IPictureDisp) As System.Drawing.Image
	Dim oImage As System.Drawing.Image
	If pictureDisp IsNot Nothing Then
		If pictureDisp.Type = 1 Then
			Dim hpalette As IntPtr = New IntPtr(pictureDisp.hPal)
			oImage = oImage.FromHbitmap(New IntPtr(pictureDisp.Handle), hpalette)
		End If
		If pictureDisp.Type = 2 Then
			oImage = New System.Drawing.Imaging.Metafile(New IntPtr(pictureDisp.Handle), New System.Drawing.Imaging.WmfPlaceableFileHeader())
		End If
	End If
	Return oImage
End Function

The resulting image file looks pretty much identical to what I see in the Windows file explorer now, when I have it set to 'Extra Large Icons'.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

WCrihfield
Mentor
Mentor

It is also interesting that I was able to use this process, because I am still using Inventor Pro 2024.3, but that Image.FromHbitmap method is for .NET 9, according to its online documentation page, and I know that Inventor 2025 moved from .Net Framework to .NET 8.  Maybe it is working for me because I also have Visual Studio 2022 Community installed.  Within the iLogic rule I am running the code from, it does not offer any 'Intellisense' pop-up hint for that method, as if it does not recognize it, but it must be working OK, because I have tested it multiple times now, and it has been producing good image files so far.  Now I wander if others may not be able to use this process if they do not have Inventor 2025 yet, and do not have Visual Studio installed either.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

chris
Advisor
Advisor

@ejaL9K8H What is the use case for saving out the thumbnail?

0 Likes

chris
Advisor
Advisor

@Michael.Navara can you change your above code to include the current view of the part. For example, I have mine set to a "Front" View zoomed into a specific distance to fill the screen, can I make it save that? Or better yet, could it be set to save out all 6 cube views? (top, bottom, left, right, front, back)?

 

chris_0-1726005782898.png

 

0 Likes

ejaL9K8H
Advocate
Advocate
Hi
We are trying to make a automated sales document directly from a Factory layout into a Word document, and we would like to show the thumbnail in this sales document.
0 Likes

chris
Advisor
Advisor

Renders look so much better. Our old catalogs used CAD views, once I was hired our catalog images went to this, still using the CAD data. If you guys would like to see what your CAD would look like rendered, just PM me

chris_0-1726033022139.png

 

0 Likes

ejaL9K8H
Advocate
Advocate

Thanks everyone!
I adjusted the code to loop through an assemply. 
If anyone else needs it

AddReference "stdole"
AddReference "System.Drawing"
Imports System.Drawing
Imports System.Drawing.Imaging
Sub Main
	' Define the directory to save images
	Dim savePath As String = "C:\TEMP\"
	
	'Dim oDoc As Document = ThisDoc.Document
	
	' Get the active document
	Dim oAsmDoc As AssemblyDocument
	oAsmDoc = ThisApplication.ActiveDocument
	Dim oThumbnail As stdole.IPictureDisp '= oDoc.Thumbnail
	Dim oPicture As System.Drawing.Image '= PictureDispToImage(oThumbnail)
	Dim sNewFile As String ' = System.IO.Path.ChangeExtension(imagePath & oOccurrenceName & ".png")
	
	For Each oOccurrence In oAsmDoc.ComponentDefinition.Occurrences
	    ' Check if the occurrence is a part document (.ipt)
	    If oOccurrence.DefinitionDocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
			
	    ' Get the properties
		oPartDoc = oOccurrence.Definition.Document
	    'oTitle = oPartDoc.PropertySets.Item("Inventor Summary Information").Item("Title").Value
	    'oAuthor = oPartDoc.PropertySets.Item("Inventor Summary Information").Item("Author").Value
		
		oThumbnail = oPartDoc.PropertySets.Item("Inventor Summary Information").Item("Thumbnail").Value
		oPicture = PictureDispToImage(oThumbnail)
		
		If oPicture IsNot Nothing Then
			oPicture = New Bitmap(oPicture, New Size(oThumbnail.Width, oThumbnail.Height))
			sNewFile = savePath & Replace(oOccurrence.Name,":","-") & ".png"
			oPicture.Save(sNewFile, System.Drawing.Imaging.ImageFormat.Png)
			Logger.Info("Save Document Thumbnail To Image File Worked!")
		Else
			Logger.Debug("Save Document Thumbnail To Image File Failed!")
		End If
		
		Else
			Logger.Debug("Document is not a Part-document!")
		End If
	Next
End Sub

Public Shared Function PictureDispToImage(pictureDisp As stdole.IPictureDisp) As System.Drawing.Image
	Dim oImage As System.Drawing.Image
	If pictureDisp IsNot Nothing Then
		If pictureDisp.Type = 1 Then
			Dim hpalette As IntPtr = New IntPtr(pictureDisp.hPal)
			oImage = oImage.FromHbitmap(New IntPtr(pictureDisp.Handle), hpalette)
		End If
		If pictureDisp.Type = 2 Then
			oImage = New System.Drawing.Imaging.Metafile(New IntPtr(pictureDisp.Handle), New System.Drawing.Imaging.WmfPlaceableFileHeader())
		End If
	End If
	Return oImage
End Function

 

0 Likes

ejaL9K8H
Advocate
Advocate
Hi Chris
Renders looks amazing!
But aren't renders a time-consuming process? It will be too computer demanding if I have to make renders of 50-200 parts in a assemply?
0 Likes

Michael.Navara
Advisor
Advisor

Hi @WCrihfield. Thank you for improvement of image ratio. 

Don't afraid of usage method  Image.FromHbitmap. It was introduced in .NET Framework 1.1 (Released  2003-07-10)

This method is just one of a few in my PictureDispConverter which I have been using for many years.

Values of IPictureDisp.Type are described in PICTYPE Constants.

0 Likes

Michael.Navara
Advisor
Advisor

Hi @ejaL9K8H.

If you want to generate images for technical documentation, it is much better to create images from Camera object. There you can setup view which is the best for current situation. You can set direction of view, image size, background color (include transparent for PNG), etc. Also you can generate more than one image for each component and designer of documentation can choose the best one.

TIP: For too long items like conveyors or beams you can create image in direction of diagonal of RangeBox. It produces much better result then standard isometric view.

WCrihfield
Mentor
Mentor

Thanks for the additional information and links @Michael.Navara.  That also cleared up a secondary issue I was seeing with that method, where I was trying to use it for retrieving ButtonDefinition.LargeIcon & ButtonDefinition.StandardIcon to image files.  Years ago I used to do this with a relatively simple looking VBA macro, which used the older system, for helping create new, slightly different icons for my own macros & rule buttons.  Then I switched to a newer iLogic rule based system, but did not like that system very well.  Using the information you linked to, and a technique from your linked method, I modified my iLogic rule based tool for extracting command icons to properly sized and properly named image files, ready for minor edits then reuse in my other rule button icons.  I will attach that tool here as a text file also, for reference, because it may be helpful/useful/informational to others too.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

chris
Advisor
Advisor

@ejaL9K8H  They can be, I rendered out almost 1000 parts and all the large Pump skid assemblies, it took some time, but I leveraged all the CAD models I created. It's mainly a quality issue. Plus you can create "models" that can be rotated on the webpage. IMO, it's just better and cheaper than hiring a photographer or buying everything needed to take product photos, which I do as well, but I'd rather just get or make the model and render it

0 Likes