Visual Basic 6 - PictureBox Image Data

dberner9 used Ask the Experts™
I need to be able to access the image data per pixel (RGB or Indexed and Palette) from a PictureBox or Image control's drawing area in Visual Basic 6; preferably both.  However, no methods seem to provide the ability to read from the control; there are only methods to draw to it.

Please respond ASAP.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
use the GetPixel() API.

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

For x = 1 To Picture1.ScaleWidth
    For y = 1 To Picture1.ScaleHeight
        msgbox "Pixel at " & x & "," & y & " : " & GetPixel(Picture1.hdc, x, y)
    Next y
Next x

Good Luck

Using the same loop above, you can also use the Point method of the Picturebox control:

"Point Method

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

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

use .

picture1.Point (x, y)


Point Method

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


object.Point(x, y)

The Point method syntax has these parts:

Part Description
object Optional. An object expression that evaluates to an object in the Applies To list. If object is omitted, the Form object with the focus is assumed to be object.
x, y Required. Single-precision values indicating the horizontal (x-axis) and vertical (y-axis) coordinates of the point in the ScaleMode property of the Form or PictureBox. Parentheses must enclose the values.


If the point referred to by the x and y coordinates is outside object, the Point method returns -1.



Does the API function offer any speed advantage over the point method?  I would guess the point method calls the API function, so perhaps there's the overhead of a single function call...  Let me know your thoughts.

>>I would guess the point method calls the API function

That would be my guess
dberner9: I use the API function because it is faster espeically when you need to get every pixel on a large image, and yes I do belive the .point calls the API.



OK- I think there's a function to extract the red, green, and blue (like an inverse of RGB) from a long integer returned by the function, but I don't know what it is.  

Use Hex to get the representation of the number into Hex format, e.g.,

strHex = Hex(Picture1.Point(x,y))

You'll get a string six characters long where the first two characters are the red value, the next two characters the green value, and the last two characters the blue value.  These are in Hex, so to get the red value in decimal, you could do:

lngRedValue = CLng("&H" & Left$(strHex, 2))


Brian:  Is there a SetPixel counterpart to GetPixel?  .PSet is too slow for my application.  


AzraSound - Need something faster than a numeric-to-string-back-to-numeric conversion.  I am using the following code right now but I am not sure it is correct:

Private Function getRed(color As Long) As Long
  getRed = (color / 65536) Mod 256
End Function

Private Function getGreen(color As Long) As Long
  getGreen = (color / 256) Mod 256
End Function

Private Function getBlue(color As Long) As Long
  getBlue = color Mod 256
End Function
Indeed there is a SetPixel API.

Public Declare Function SetPixel Lib "gdi32" Alias "SetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

dim color as long
dim retval as long
dim x as long
dim y as long
color = vbred
x = 150
y = 150
retval = setpixel(picture1.hdc,x,y,color)

Good Luck

Someone mentioned converting from long integer to RGB values, I think that when youve converted to RGB youll find that its in reverse order, BGR

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial