By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,042 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,042 IT Pros & Developers. It's quick & easy.

How to read bmp image data in VB.Net

P: 1
I'm working on image recognition which needs to be fast. From what I know the speed of using a memory stream is much faster than using .GetPixel for bitmaps

But when I use the memory stream I'm getting different results. I'm sure it's to do with how I'm creating and reading the stream. Can anyone assist with the following code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ScanImageShape(ByRef bMap As Bitmap)
  2.         Dim lOffset As Long     'The length of the bmp header
  3.         Dim lPos As Long
  4.         Dim x As Integer
  5.         Dim y As Integer
  6.  
  7.         'Get a memory stream into a byte array.
  8.         Dim imgByteArray() As Byte
  9.         Dim msBitmap As New IO.MemoryStream
  10.  
  11.         Dim bRed As Byte
  12.         Dim bGreen As Byte
  13.         Dim bBlue As Byte
  14.         Dim cPixel As Color
  15.  
  16.         'Put the bitmap into the memory stream
  17.         bmap.Save(msBitmap, ImageFormat.Bmp)
  18.  
  19.         'Load memory stream into byte array
  20.         imgByteArray = msBitmap.GetBuffer
  21.  
  22.         'This is the starting byte position of the bitmap data, after the header (???)
  23.         lOffset = 54
  24.         For y = 0 To bmap.Height - 1  '240
  25.             For x = 0 To bmap.Width - 1  ' 320
  26.                 'Assume that there are 4 bytes per pixel
  27.                 lPos = (y * 96) + (x * 4) + lOffset
  28.                 cPixel = bmap.GetPixel(x, y)
  29.  
  30.                 bBlue = imgByteArray(lPos).ToString
  31.                 bGreen = imgByteArray(lPos + 1).ToString
  32.                 bRed = imgByteArray(lPos + 2).ToString
  33.  
  34.                 'Insert BYTE values into a multiline text box
  35.                 txtDebugCoords.AppendText(lPos.ToString + ", " + _
  36.                                           x.ToString + ", " + _
  37.                                           y.ToString + ", " + _
  38.                                           bBlue.ToString + ", " + _
  39.                                           bGreen.ToString + ", " + _
  40.                                           bRed.ToString + Environment.NewLine)
  41.  
  42.  
  43.                 'In another multiline text box compare the "getpixel" values. Should be the same.
  44.                 txtDebugBMP.AppendText(bmap.GetPixel(x, y).B.ToString + ", " + _
  45.                                        bmap.GetPixel(x, y).G.ToString + ", " + _
  46.                                        bmap.GetPixel(x, y).R.ToString + Environment.NewLine)
  47.  
  48.             Next
  49.         Next
  50.  
  51.         msBitmap = Nothing
  52.  
  53.     End Sub
Aug 4 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.