Hi @benedikt_seitzLXJD2. I finally found time to revisit this topic this morning. If your main goal is to get a screen captured images of what is showing in Inventor's active View, and you need it to be square, instead of its natural rectangular shape, then there are likely other ways of achieving that which are less complicated. Inventor's API has multiple methods for capturing images of what is showing within its views. When we use those methods, we can specify the size of the image we want it to create, and the type of image file it will create. Sometimes we can even dictate the 'background', or make it transparent, when it captures the image. And if that were not enough, we could always just edit the resulting image after it is created too, if needed. That can be done entirely by code, if needed. The methods for creating images directly associated with an Inventor View are View.SaveAsBitmap, View.SaveAsBitmapWithOptions, and View.SaveAsBitmapWithRayTracing. Then there are similar methods of the Camera object that we can get from the View through its View.Camera property. The Camera.CreateImage & Camera.CreateImageWithOptions methods are both for creating IPictureDisp type images, for active internal digital use. But the Camera.SaveAsBitmap & Camera.SaveAsBitmapWithOptions methods are similar to the View methods, but with a couple more options for dictating specific color backgrounds.
Beyond that, there are a lot of code examples available online for editing or modifying existing images.
I will post an example below, for starters, but it will be in vb.net, which can be used in an iLogic rule, not in VBA. I stopped using VBA several years ago, around the same time Autodesk stopped including it in their standard installations for security purposes, because Microsoft stopped maintaining it. The VB.NET coding system used in our iLogic rules has a lot of similarities, because it is still in the 'Visual Basic' language family, but is much newer, and offers more functionality and advantages. If you are using an older version of Inventor where VBA macros were the only way to put a custom button into Inventor's ribbon, then I can understand that, but a VBA macro can simply call an iLogic rule to run, instead of containing all the code for the task in VBA. The last several versions of Inventor have allowed us to put buttons in Inventor's ribbons that will run external iLogic rules or show global iLogic Forms, which was long anticipated in the Inventor automation world.
I wrote the 'Main' block of code, and customized some of the other blocks of code a bit, but the core functionality of the image editing block of code I got from GPT 5.4 nano (an AI source). I tested it locally on one of my Inventor models. It saved a screen shot image of my model to a PNG file with a transparent background first, that was the full size of my View. Then it opened that image file invisibly, and cropped it to a square, using the smaller dimension of either the original images width or its height, as the size for both sides of the square, then saved the result to a new file. The second image was identical to the first image, and one of its dimensions is the same as the original, but with the longer dimension cropped towards the center of the image. That sounds like what you were hoping to achieve, so I hope this will help you reach your goal. And maybe help some others along the way too.
AddReference "System.Drawing"
Imports System.Drawing
Imports System.Drawing.Imaging
Sub Main
'hard-coded full file names...this can be changed later, if needed
'could show an interactive file browser here instead
Dim sOriginalImageFile As String = "C:\Temp\Inventor ScreenShot.png"
Dim sCroppedImageFile As String = "C:\Temp\Inventor ScreenShot - Cropped To Square.png"
'get Inventor's active View (assume the view is ready for screen capture)
Dim oView As Inventor.View = ThisApplication.ActiveView
'specify an Option for the image to have a transparent background
'this only works for the PNG or TIFF Image types, not for GIF, BMP, or JPG
Dim oOptions As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
oOptions.Value("TransparentBackground") = True
'using zero values (default) gets the entire view width & height
oView.SaveAsBitmapWithOptions(sOriginalImageFile, 0, 0, oOptions)
'allow Inventor to finish processing stuff in its queue
ThisApplication.UserInterfaceManager.DoEvents()
'call the custom Sub routine to Crop the image file from its original rectangular shape,
'to a centered square, without squishing or deforming the image
CropImageToCenteredSquare(sOriginalImageFile, sCroppedImageFile)
End Sub
Sub CropImageToCenteredSquare(imageFile As String, _
Optional resultImageFile As String = Nothing)
If String.IsNullOrWhiteSpace(resultImageFile) Then
resultImageFile = imageFile
End If
Using oOrigImg As System.Drawing.Image = System.Drawing.Image.FromFile(imageFile)
Dim dW As Integer = oOrigImg.Width
Dim dH As Integer = oOrigImg.Height
Dim dMinDim As Integer = Math.Min(oOrigImg.Width, oOrigImg.Height)
Dim dX_Ctr As Integer = (dW - dMinDim) / 2
Dim dY_Ctr As Integer = (dH - dMinDim) / 2
Using bmp As New Bitmap(dMinDim, dMinDim, PixelFormat.Format24bppRgb)
Using g As Graphics = Graphics.FromImage(bmp)
g.CompositingMode = Drawing2D.CompositingMode.SourceCopy
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
g.DrawImage(oOrigImg, New Rectangle(0, 0, dMinDim, dMinDim), _
New Rectangle(dX_Ctr, dY_Ctr, dMinDim, dMinDim), GraphicsUnit.Pixel)
'call other custom Sub routine to save this modified image to file
SaveImageToFileByExtension(bmp, resultImageFile)
End Using 'g Graphics
End Using 'bmp
End Using 'oOrigImg
End Sub
Sub SaveImageToFileByExtension(img As System.Drawing.Image, outPath As String)
Dim sExt As String = System.IO.Path.GetExtension(outPath).ToLowerInvariant()
Select Case ext
Case ".png"
img.Save(outPath, ImageFormat.Png)
Case ".bmp"
img.Save(outPath, ImageFormat.Bmp)
Case ".gif"
img.Save(outPath, ImageFormat.Gif)
Case ".tif", ".tiff"
img.Save(outPath, ImageFormat.Tiff)
Case Else
img.Save(outPath, ImageFormat.Jpeg)
End Select
End Sub
The image editing code could even take it a step further, if needed (with more complex code), to crop as much of a solid or transparent background away as possible, while still keeping the image square, and can even leave a margin of space around it.
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)