How to read the pixels of a bitmap to determine it's predominant color.

Posted on 2006-06-08
Last Modified: 2008-03-17
I have a host of bitmap files basically all B&W drawings. Some are black with white drawings & some are white with black drawing.
I need a way to find out the predominant pixel color (background) using Office VB.

Question by:dmerrow
    LVL 29

    Expert Comment

    You have to remember that what your eyes see as white or black on a computer image doesn't mean that its actually white in computer colors.
    LVL 29

    Accepted Solution

    The following code below assumes that you are looking for PURE black and PURE white. I leave the math part up to you for checking which is greater (white or black)

    'Copy paste into form
    'Add 1 command button

    Option Explicit

    Private Type BitmapInfo
        bmType As Long
        bmWidth As Long
        bmHeight As Long
        bmWidthBytes As Long
        bmPlanes As Integer
        bmBitsPixel As Integer
        bmBits As Long
    End Type

    Private Declare Function CreateCompatibleDC Lib "gdi32" ( _
        ByVal hdc As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" ( _
        ByVal hdc As Long, _
        ByVal hObject As Long) As Long
    Private Declare Function GetObjectA Lib "gdi32" ( _
        ByVal hObject As Long, _
        ByVal nCount As Long, _
        lpObject As Any) As Long
    Private Declare Function DeleteDC Lib "gdi32" ( _
        ByVal hdc As Long) As Long

    Private Declare Function DeleteObject Lib "gdi32" ( _
        ByVal hObject As Long) As Long

    Private Declare Function GetPixel Lib "gdi32" ( _
        ByVal hdc As Long, _
        ByVal x As Long, _
        ByVal y As Long) As Long

    Private Declare Function GetInputState Lib "user32" () As Long

        Dim BMI As BitmapInfo
        Dim PiC As StdPicture
        Dim DC  As Long
        Dim PX  As Long
        Dim w   As Long
        Dim h   As Long
        Dim White As Long
        Dim Black As Long
        Public Sub Scan(inImg As Variant)
            Black = 0
            White = 0
            Set PiC = LoadPicture(inImg)
            DC = CreateCompatibleDC(0)
            Call SelectObject(DC, PiC.Handle)
            Call GetObjectA(PiC.Handle, LenB(BMI), BMI)
            For w = 0 To BMI.bmWidth
              For h = 0 To BMI.bmHeight
                PX = GetPixel(DC, w, h)
                Select Case PX
                    Case vbWhite
                        White = White + 1
                    Case vbBlack
                        Black = Black + 1
                End Select
                If GetInputState Then
                End If
              Next h
            Next w
        DeleteDC DC
        DeleteObject PiC.Handle
        Set PiC = Nothing
        Debug.Print Black & " black pixels found"
        Debug.Print White & " white pixels found"
        End Sub

    Private Sub Command1_Click()
        Scan "C:\test.jpg" ' image you want to scan... gif,jpg,bmp
    End Sub
    LVL 3

    Author Comment



    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now