-
Bitmaps into arrays
Argh! It always comes to this when I'm working with images. Usually, when I need to programatically interpret data in a bitmap, I do it graphically, using GetPixel in a PictureBox. But that's slow and inefficient.
How can I load each pixel of a bitmap into a 2-dimensional array? Say the bitmap is 10 pixels by 10 pixels: the array would be Bitmap(10,10).
Each item in the array would contain a long value indicating the color at that location in the file.
Can someone help me with this?
Thanks!
-
1 Attachment(s)
Re: Bitmaps into arrays
Here's what you're looking for...
It should explain everything you need to know about bitmap images on a raw binary level...
It's just basic theory, but you can adapt the code to basically anything bitmap related...
Hope this helps... enjoy...
-
Re: Bitmaps into arrays
Thank you, I'll take a look.
-
Re: Bitmaps into arrays
I keep getting a 'Subscript out of Range' error...
I haven't tried to understand the code yet, but what exactly does it do? Does it open an existing bitmap and interprets it or does it create a bitmap file?
-
Re: Bitmaps into arrays
Basically what the program does is convert a bitmap into 24bit depth...
The reason I gave you the file (even tho it's irreleveant) is that it loads the bits for the bitmap directly from the file i.e. it stores the pixel location and the colour of the pixel directly into the array, without having to open the image and do a pixel scan....
That way it's much faster...
As for the error, I'll check it out and get back to you...
-
Re: Bitmaps into arrays
Alright, thanks. (There's an invalid label, too, but all you have to do is change "ErrHandler" to "ErrHandle" in the GoTo statement).
Now what is 24bit depth? Well, obviously, 24 bits of color but -- argh, I'm confused. I did some research, but it didn't help...
-
Re: Bitmaps into arrays
lol...
Don't worry about the conversion...
Just take a look at the part where the data is loaded into the arrays....
After a while at staring at it it begins to make sense...
-
Re: Bitmaps into arrays
Alright... I will. Thanks
-
Re: Bitmaps into arrays
Hmm, actually...
Doesn't VB have any built-in function (or API) for getting the RGB values for each pixel in a BMP?
-
Re: Bitmaps into arrays
Yes, but you still have to load the picture onto a device context...
-
Re: Bitmaps into arrays
i don't know whether this can help yo.
I was working on steganography and had similar problems to get the RGB values from a pixel.
After searching i got this code from the net. Check if this helps!!!!
--------------------------------------------------------------------------
'GET RED COMPONENT
Public Function GetRedValue(ByVal Color As Long) As Byte
GetRedValue = CByte(Color And &HFF)
End Function
--------------------------------------------------------------------------
'GET GREEN COMPONENT
Public Function GetGreenValue(ByVal Color As Long) As Byte
GetGreenValue = CByte((Color And 65280) \ 256)
End Function
--------------------------------------------------------------------------
'GET BLUE COMPONENT
Public Function GetBlueValue(ByVal Color As Long) As Byte
GetBlueValue = CByte((Color And &HFF0000) \ 65536)
End Function
The color arguement is the colour of the current pixel that you get from the GetPixel method of "gdi32" library...
-
Re: Bitmaps into arrays
I would create a table on the fly, and then convert the unique colors in the table. You'd still have to loop thru all pixels, though.