Thanks @g.georgiades, I liked that too. I was familiar with a couple other ways of obtaining the Image object, then using the Image.Save method, but I do not recall seeing that Icon.FromHandle(IPictureDisp.Handle).ToBitmap Method combination before. I like it. The Regex portion is a bit hard to read, so I usually use something like the following for deriving the file name from the ControlDefinition.InternalName property value. Maybe you will like it too. It is likely not as efficient as using Regex, but is definitely shorter, and a little bit easier to read/understand later.
'replace any invalid file name characters with a single space
fileName = String.Join(" ", cd.InternalName.Split(System.IO.Path.GetInvalidFileNameChars()))
I used both to quickly create a similar Boolean Function for processing a single ControlDefinition to export one of its icons as a PNG. I will post that here also, for others to reference as well. Naturally, I designed it in a way it could be used in a regular iLogic rule, but I did take the time to add in some helpful method & and method parameter documentation. Then added a simplistic Sub Main block in there so that folks could quickly & easily test it.
Imports Inventor
Imports System.Drawing
Imports System.IO
Imports System.Runtime.InteropServices
Imports Autodesk.iLogic.Interfaces
AddReference "System.Drawing.dll"
AddReference "stdole.dll"
Sub Main
Dim oCD As Inventor.ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("iLogic.Configuration")
Dim sImage_File As String
Dim bWorked As Boolean = SaveControlDefinitionIconToImageFile(oCD, , , sImage_File)
Logger.Info(vbCrLf & "Image File: " & sImage_File)
End Sub
''' <summary>
''' Saves either the ControlDefinition's Large or Standard Icon as an external PNG file
''' </summary>
''' <param name="cd">The source Inventor.ControlDefinition to get the Icon from</param>
''' <param name="filePath">The suggested file path for the new PNG file</param>
''' <param name="fileName">The suggested file name, without extension, for the new PNG file</param>
''' <param name="FullFileName">Returns the resulting full path & file name, with extension</param>
''' <returns>A Boolean indicating if this method was successful (True = successful)</returns>
Public Function SaveControlDefinitionIconToImageFile(ByVal cd As Inventor.ControlDefinition, _
Optional ByVal filePath As String = Nothing, _
Optional ByVal fileName As String = Nothing, _
Optional ByRef FullFileName As String = Nothing) As Boolean
'<<< verify or set filePath
If String.IsNullOrWhiteSpace(filePath) Then
filePath = "C:\Temp\Inventor ControlDefinition Icon Image Files\"
End If
'<<< make sure filePath exists
If Not System.IO.Directory.Exists(filePath) Then
Try
System.IO.Directory.CreateDirectory(filePath)
Catch
Logger.Error(vbCrLf & "Could Not Create Following Directory: " & vbCrLf & filePath)
Return False
End Try
End If
'<<< verify or set fileName
If String.IsNullOrWhiteSpace(fileName) Then
'replace any invalid file name characters with a single space
fileName = String.Join(" ", cd.InternalName.Split(System.IO.Path.GetInvalidFileNameChars()))
End If
'<<< combine filePath, fileName, and fileExtension to set value of ByRef FullFileName param
FullFileName = System.IO.Path.Combine(filePath, fileName & ".png")
'<<< try to obtain image data from one of the ControlDefinition's Icons
Dim oIcon As stdole.IPictureDisp = Nothing
Try : If (cd.LargeIcon IsNot Nothing) Then oIcon = cd.LargeIcon
Catch : End Try
If oIcon Is Nothing Then
Try : If (cd.StandardIcon IsNot Nothing) Then oIcon = cd.StandardIcon
Catch : End Try
End If
If oIcon Is Nothing Then Return False
'<<< try to create an Icon from above image, then convert it to a Bitmap (a type of Image)
Using oImage As Image = Icon.FromHandle(oIcon.Handle).ToBitmap()
'<<< try to save this Image to an external PNG file
Try
oImage.Save(FullFileName, System.Drawing.Imaging.ImageFormat.Png)
Return True
Catch
Logger.Error(vbCrLf & "Error saving Image to following PNG file:" & vbCrLf & FullFileName)
Return False
End Try
End Using
End Function
Wesley Crihfield

(Not an Autodesk Employee)