distance between two pixels using VB.

Posted on 2001-07-18
Last Modified: 2010-08-05
   I have a BW scanned image that is saved as a bmp, basically I want to figure out it's length by measuring the top-most black pixel and the bottom pixel.  How do I find these pixels?  I also would like to find the width at a certain number of inches, pixels, or whatever down the image.  How do I do this also?  I figure it would be easy if I started at the top left pixel and scaned row by row till I found a black one but I have no idea how to do this.  Can someone help me?

Thanks in Advance,
Question by:Cide
LVL 22

Accepted Solution

rspahitz earned 150 total points
ID: 6296056
Point Method

Returns, as a long integer, the red-green-blue (RGB) color of the specifiedpoint on a Form or PictureBox. Doesn't supportnamed arguments.


object.Point(x, y)

Use a loop something like this:

for y=0 to Picture1.height
  for x=0 to Picture1.width
    if Picture1.Point(x,y) = vbBlack then
      msgbox "Found it at x,y= " & x & "," & y, vbokonly
      y = Picture1.height ' force exit of the row loop
      exit for
    end if
  next x
next y


Expert Comment

ID: 6296575

Here's a file over the net for you:


Description: I just made a couple changes to this.I am new at programming so this is my first shot so i hope i don't blow it:) This program lets you open a picture file and it tells you the height and width of the picture in pixels. I think this would come in very handy in making webpages so you'll know the exact dimensions of the pic. If it is really useless let me know. Thx

That's it!

glass cookie : )

Expert Comment

ID: 6297083
Note about rspahitz's code. Change the picbox's scalemode to pixels. It'll run about 225 (15x15) times faster that way. Also, you'll need a backwards loop to search up from the bottom. Same code, but with two changes:

for y=Picture1.height to 0 step -1

     y = 0 ' force exit of the row loop

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 6297088
To get the width, just pick a row (second parameter of Point) to select points from and search across (inner For-Next loop). Then do the same on the same row but look backwards from the other end.
LVL 22

Expert Comment

ID: 6299282
Thanks, KDivad, I forgot about the "bottom" and "right" request.

Author Comment

ID: 6299479
Wow works like a charm thanks guys!

Kdivad thnaks additionally for your extra help, went so much faster with your "tweaks".  If you want some points I can put then on a sperate question just ask me.

Expert Comment

ID: 6299608
We all have to forget something sometimes. Gives the rest of us a chance to brag a bit. <grin>

Don't worry about it. I may need your help someday.


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bit defender blocks good applications 2 100
Adding to a VBA? 6 76
Automatic Email Reminder 4 56
VBA: copy range dynamically based on config sheet v2 3 48
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

766 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