21 | 08 | 2017

Create Screenshot With White Background and add to Clipboard

The code below makes a screenshot of a part or an assembly. Before it makes the screenshot it changes the background color to white. The image is saved at the therefore given folder and added to the clipboard. After the screenshot has been made the background will be set back to it's original settings. 

 

    Public Sub ScrShotExecute()
        On Error GoTo errorhandler

        'THIS FUNCTION MAKES A SCREENSHOT FROM THE ACTIVE PART OR ASSEMBLY AND SAVES IT TO THE DESIGNATED FOLDER.
        Dim Filepath As String
        Dim oDoc As Object
        Dim Filename
        oDoc = AddinGlobal.InventorApp.ActiveDocument

        Filepath = "C:\TestImage.jpg" 'ENTER SAVE PATH

        Dim m_inventorApplication As Object
        m_inventorApplication = AddinGlobal.InventorApp
        oDoc = m_inventorApplication.ActiveDocument

        Dim oOldBGType As Inventor.BackgroundTypeEnum = m_inventorApplication.ColorSchemes.BackgroundType 'Get the active background type
        Dim oOldBGImage As String = ""

        'SET THE BACKGROUND TO WHITE
        'in drawing document, change the sheet color and snapshot
        If TypeOf (oDoc) Is DrawingDocument Then
            Exit Sub 'No screenshot is taken when an active document is a drawing
        Else 'other type of document, set color info of color skema


            If oOldBGType = BackgroundTypeEnum.kImageBackgroundType Then
                oOldBGImage = m_inventorApplication.ActiveColorScheme.ImageFullFileName
            End If

            'Make the background white using a bitmap
            m_inventorApplication.ColorSchemes.BackgroundType = BackgroundTypeEnum.kImageBackgroundType
            m_inventorApplication.ActiveColorScheme.ImageFullFileName = "c:\white.bmp" 'add a location to a small bitmap image with only white color
            m_inventorApplication.ActiveView.Update()

            'SAVE FILE
            oDoc.SaveAs(Filepath, True)

            'set the color back
            m_inventorApplication.ColorSchemes.BackgroundType = oOldBGType
            If oOldBGType = BackgroundTypeEnum.kImageBackgroundType Then
                m_inventorApplication.ActiveColorScheme.ImageFullFileName = oOldBGImage
            End If


        End If

        'ADD TO CLIPBOARD
        Dim sExtension As String
        Dim img As System.Drawing.Image
        sExtension = System.IO.Path.GetExtension(Filepath)
        If Trim(sExtension) = ".bmp" Or _
            Trim(sExtension) = ".jpg" Or _
            Trim(sExtension) = ".jpeg" Or _
            Trim(sExtension) = ".gif" Or _
            Trim(sExtension) = ".png" Then
            img = Image.FromFile(Filepath)

            My.Computer.Clipboard.SetImage(img)

            MsgBox("Image is copied to the clipboard and saved at " & vbCr & Filepath, vbInformation)
        End If

        Exit Sub

errorhandler:
        MsgBox(Err.Description)
        'set the color back
        m_inventorApplication.ColorSchemes.BackgroundType = oOldBGType
        If oOldBGType = BackgroundTypeEnum.kImageBackgroundType Then
            m_inventorApplication.ActiveColorScheme.ImageFullFileName = oOldBGImage
        End If

    End Sub
Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2330580
Latest Articles