Testing the pixels of a BMP in memory

My app reads a BMP file from the Hard drive and has to test the color of each pixel. I used to read byte after byte from the bmp file but the result is very very slow. I think I should load the bmp into the memory and test the pixels in it but how to load the file quickly enough and how to access to the properties of each pixel ? Thanks for your help.
PleinpopossumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
You can read a chunk of bytes at the same time using code like this

ReDim ByteArray(10)

Open filename For Binary As #1
Get #1, , ByteArray()
Close #1

This will read 11 bytes (if you have 0-based arrays (default)).
0
watyCommented:
This is for setting, but you can access each pixel

' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 28/06/99
' * Time             : 12:26
' **********************************************************************
' * Comments         : Changing a VB Picture Object Pixel by Pixel
' *
' *
' **********************************************************************

Public Type SAFEARRAYBOUND
   cElements  As Long
   lLbound    As Long
End Type

Public Type SAFEARRAY1D
   cDims          As Integer
   fFeatures      As Integer
   cbElements     As Long
   cLocks         As Long
   pvData         As Long
   Bounds(0 To 0) As SAFEARRAYBOUND
End Type

Public Type SAFEARRAY2D
   cDims          As Integer
   fFeatures      As Integer
   cbElements     As Long
   cLocks         As Long
   pvData         As Long
   Bounds(0 To 1) As SAFEARRAYBOUND
End Type

Public Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Public Type BITMAP
   bmType         As Long
   bmWidth        As Long
   bmHeight       As Long
   bmWidthBytes   As Long
   bmPlanes       As Integer
   bmBitsPixel    As Integer
   bmBits         As Long
End Type

Public Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long

Then you can transform your stdPicture variable in a array variable like this:

Private Sub MySub()

   ' these are used to address the pixel using matrices
   Dim pict() As Byte
   Dim MyPictureVar as stdPicture

   Dim sa As SAFEARRAY2D, bmp As BITMAP

   MyPictureVar=LoadPicture(app.path & "\MyImage.BMP")
   ' get bitmap info
   GetObjectAPI Pictbox.Picture, Len(bmp), bmp 'dest
   ' exit if not a supported bitmap
   If bmp.bmPlanes <> 1 Or bmp.bmBitsPixel <> 8 Then
      MsgBox " 256-color bitmaps only", vbCritical
      Exit Sub
   End If

   ' have the local matrix point to bitmap pixels
   With sa
      .cbElements = 1
      .cDims = 2
      .Bounds(0).lLbound = 0
      .Bounds(0).cElements = bmp.bmHeight
      .Bounds(1).lLbound = 0
      .Bounds(1).cElements = bmp.bmWidthBytes
      .pvData = bmp.bmBits
   End With
   CopyMemory ByVal VarPtrArray(pict), VarPtr(sa), 4

   For c = 0 To UBound(pict, 1)
      'make this column black
      For r = 0 To UBound(pict, 2)
         pict(c, r) = 0 'Black Color
      Next
   Next

   ' clear the temporary array descriptor
   ' without destroying the local temporary array
   CopyMemory ByVal VarPtrArray(pict), 0&, 4

End Sub


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
watyCommented:
Also this one
' #VBIDEUtils#************************************************************
' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 26/04/99
' * Time             : 11:07
' **********************************************************************
' * Comments         : Change color of a pixel in a picturebox
' *
' *
' **********************************************************************

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

' Place this code in Picture1_MouseDown(Button As...)

' This will replace the pixel at x:10, y:10 with a red pixel.
Dim s As Long
s = setpixel(Picture1.hDC, 10, 10, rgb(255,0,0))
Picture1.Refresh

0
watyCommented:
0
PleinpopossumAuthor Commented:
Thanks a lot ! I'll do my best to apply these new knowleges.

  Hope to be able to help you anyday...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.