AnsweredAssumed Answered

Want to count colour in a Saved out Bitmap

Question asked by Kelson Lachance on Jan 11, 2015
Latest reply on Jan 15, 2015 by Kelson Lachance

I am still using the VB editor that came with Solidworks.


I think I don't have the correct reference libraries.


Just trying to save out a bitmap and count the number of pixels of a certain color.  This was the start; but it doesn't recognize the Bitmap calls?


Dim swApp As Object

Option Explicit
' Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim returnVal As Boolean
Dim fileName As String
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    fileName = "c:\temp\cstick.bmp"
    ' Save as bitmap and use current window size
    returnVal = swModel.SaveBMP(fileName, 0, 0)
    getAverageColor ("c:\temp\cstick.bmp")
End Sub

Private Function getAverageColor(ByVal imageFilePath As String)
    Dim bmp As Bitmap
    Dim totalR As Integer
    Dim totalG As Integer
    Dim totalB As Integer
    Dim x As Integer
    Dim y As Integer
    Dim pixel As ColorTable
    Dim totalPixels As Integer
    Dim averageR As Integer
    Dim averageg As Integer
    Dim averageb As Integer
    'Set bmp = Bitmap(imageFilePath)
    For x = 0 To bmp.Width - 1
        For y = 0 To bmp.Height - 1
            pixel = bmp.GetPixel(x, y)
            totalR = pixel.R
            totalG = pixel.G
            totalB = pixel.B
    totalPixels = bmp.Height * bmp.Width
    averageR = totalR \ totalPixels
    averageg = totalG \ totalPixels
    averageb = totalB \ totalPixels
    Debug.Print averageR
    Debug.Print averageg
    Debug.Print averageb
    'Return Color.FromArgb(averageR, averageg, averageb)
End Function