Re: Finding Color of Specific Pixel in Loaded Image
- From: "Mike Williams" <Mike@xxxxxxxxxxxxxxxxx>
- Date: Sun, 1 Jan 2006 10:34:09 -0000
<IWP506@xxxxxxxxx> wrote in message news:1136085577.996835.231780@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OK -- I have the return value. Say, for instance, it is "16711422". Some documentation I read says this is an RGB value... Are they saying red is 167 green is 114 and blue is 22?
The individual colours (red, green and blue) are contained in the lower three bytes of the returned value (which is itself a Long containing four bytes). You can "see" the individual byte data by viewing the Long in hex format. For example, the value 16711422 is FEFEFE when viewed in hex format, indicating that in this specific case all three colours (RGB) have the value &HFE (which is of course 254 in decimal). The valid range for each byte is 0 to 255, with 0,0,0, being black and 255,255,255 being white.
So, you can split the returned value into the three RGB values by converting it to a Hex string and then manipulating that string. However, that is a fairly slow way of doing it and a much faster way is to dump the returned data from the memory space of the Long directly into the memory space of a small Byte array. There are a number of ways of doing this. You could use the CopyMem API or alternatively the much safer VB Lset method. Here is an example of the latter:
Option Explicit Private Type longcolor Value As Long End Type Private Type RGBcolors Red As Byte Green As Byte Blue As Byte Dummy As Byte End Type Private myLong As longcolor Private myBytes As RGBcolors
Private Sub Command1_Click() myLong.Value = Picture1.Point(10, 10) LSet myBytes = myLong ' the following is just to show that it works Print myBytes.Red Print myBytes.Green Print myBytes.Blue End Sub
The above code performs the conversion very quickly. If you want to do it faster then you need to speed up the slow part of the whole process (which is reading the Long value from the pixel in the first place). You are currently using the VB Point method (which is the slowest way of doing it). A faster way would be to use the alternative API GetPixel routine instead. An even faster way (very much faster!) is to get the entire block of pixels for the complete image into a suitable VB array in one go and then you can read and manipilate the individual pixel colours very quickly. In fact, there is an even faster way than that but it needs quite a lot more code to do it and you need to take account of the different possible pixel data formats. For the time being I would suggest that you use the above method until you are happy that you understand the process and then post again if you want help with the much faster "get the whole lot into a suitable VB array in one go" method that I have mentioned.
Mike
.
- References:
- Finding Color of Specific Pixel in Loaded Image
- From: PulsarSL
- Re: Finding Color of Specific Pixel in Loaded Image
- From: Larry Serflaten
- Re: Finding Color of Specific Pixel in Loaded Image
- From: IWP506
- Finding Color of Specific Pixel in Loaded Image
- Prev by Date: Re: Visual Basic 2005 Express Edition
- Next by Date: Re: Visual Basic 2005 Express Edition
- Previous by thread: Re: Finding Color of Specific Pixel in Loaded Image
- Next by thread: Re: Bit-order reversal (little-endian <--> big-endian)
- Index(es):
Relevant Pages
|