By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,550 Members | 918 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,550 IT Pros & Developers. It's quick & easy.

count black & white pixels in a bitmap

P: 2
hi everyone, i am writing a program for my research project and i need to count
the number of black & white pixels in an image inside a picturebox. I am basically a beginner in visual basic. I already have a picture viewer. Please help me devise a visual basic code to count the number of black & white pixels inside the picturebox
May 28 '12 #1
Share this Question
Share on Google+
5 Replies

Expert 5K+
P: 8,434
I'm going to assume you're using the old VB, version 6 or earlier. If you're using the later dotnet-based ones (VB2008, etc), the logic may work, but any language syntax will probably be totally wrong. In that case I recommend posting your question in our VB.Net forum.

As usual in programming, there are probably lots of ways to achieve what you want. Here's one fairly simple (though possibly a bit slow) method that should work.

First of all, make sure the .Scalemode property of the picturebox is set to 3 (pixel).

Use the .Width and .Height properties (or maybe the .Scalewidth and .ScaleHeight) of the picturebox to get the limits of where you'll be looking. Experiment to see what works better.

Then do two nested loops to go across and down, and use the .Point property (or is that a method? I forget) of the picturebox to get the colour of each pixel.

Here's an experiment that might help in building an understanding of what's going on. Paste this line into the MouseMove event of the picturebox...
Expand|Select|Wrap|Line Numbers
  1. Debug.Print X, Y, Picture1.Point (X,Y)
(That's if the name of the control is Picture1, of course. :)
May 28 '12 #2

Expert Mod 100+
P: 465
I'm going on the presumption that you're using VB6 and have a solution for VB6. If you're not using VB6 (like maybe VB8) let us know so we can find a .NET solution.

First we use the Win32 API GetPixel.

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
  3. Private Sub Form_Load()
  4.     pixBox.Picture = LoadPicture("YourPicturePath")
  5.     Dim i As Long, j As Long, nBlack As Long, nWhite As Long, pHdc As Long, tempColor As Long
  6.     Show
  7.     With pixBox
  8.         pHdc = .hdc
  9.         .ScaleMode = 3
  10.         For i = 0 To .ScaleWidth
  11.             For j = 0 To .ScaleHeight
  12.                 tempColor = GetPixel(pHdc, i, j)
  13.                 If tempColor = vbWhite Then
  14.                     nWhite = nWhite + 1
  15.                 ElseIf tempColor = vbBlack Then
  16.                     nBlack = nBlack + 1
  17.                 End If
  18.             Next
  19.         Next
  20.     End With
  21.     MsgBox "White : " & nWhite & vbCrLf & "Black : " & nBlack, vbInformation
  22. End Sub 
Hope that helps :)
Jun 5 '12 #3

P: 2
thanks for the replies. I really appreciate your help to a beginner like me. I have inputted the code but some terms like the vbblack is not recognized by visual basic 2010 express. Before creating the form.vb, is there such a file as program.vb as equivalent to program.cs of visual c#? How to convert the codes above to visual basic 2010 express? Thanks
Jun 7 '12 #4

Expert Mod 100+
P: 465

That's because you posted your question in the Visual Basic 4/5/6 forum, when it should have been in the VB.NET forum (VB 2010 is VB.NET.

I'm moving this to the proper (VB.NET forum) forum, then try to work out a solution in VB.NET
Jun 7 '12 #5

Expert Mod 100+
P: 465
Here's a function for counting black pixels, should only require a few modifications to make it count both black & white (I jut wanted to help you get started)

Expand|Select|Wrap|Line Numbers
  1. Function countPixels() As Integer
  2.     Dim bmp As Bitmap = Image.FromFile(TextBox3.Text)
  4.     ' Make a difference image.
  5.     Dim w As Integer = bmp.Width ', bmp.Width)
  6.     Dim h As Integer = bmp.Height ', bm2.Height)
  8.     ' Create the difference image.
  9.     Dim count As Integer = 0
  10.     Dim blackPixel As Color
  12.     For x As Integer = 0 To w - 1
  13.         For y As Integer = 0 To h - 1
  14.             blackPixel = bmp.GetPixel(x, y)
  15.                 If blackPixel.R = 0 AndAlso blackPixel.G = 0 AndAlso blackPixel.B = 0 Then
  16.                     count += 1
  17.                 End If
  19.         Next 'y
  20.     Next 'x
  22.     bmp.Dispose()
  23.     Return count
  24. End Function
That should get you down the right path (If I did it all for you you wouldn't learn anything.
Jun 7 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.