Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-06-08
3
Medium Priority
?
284 Views
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.

Thanks!
0
Comment
Question by:dmerrow
  • 2
3 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 16865817
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.
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 2000 total points
ID: 16865854
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
                DoEvents
            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
0
 
LVL 3

Author Comment

by:dmerrow
ID: 16871053
Perfect...Thanks!

DM
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

564 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