|
-
Dec 14th, 2007, 03:02 PM
#1
Thread Starter
Member
LockBits Bitmap R,G,B reading were messed up?
Hi, I am testing the Lockbits function to read a bitmap r,g,b value.
The codes got part of the bitmap information reading correctly but part of them are just messed up (sometime the red were written into blue, or to green).
For instance, i created a bitmap with RGB(12,32,48) filling, when i read it from the following code, it sometimes is just right with a bunch of (48,43,12) reading, but a few times, in the middle of the printing, there would be a (0,0,12), then the rest is all wrong, (position shifted)
I have no idea when this happen and when it doesn't. I tried it with out bitmap and some of them were right some of them had the same problem.
Does someone encounter the same problem before? Why does it happen? Is there a pattern? How should I avoid it?
Code:
Public Function BytePixel(ByVal strBmp As String) As Byte()
Try
Dim pBITMAP As System.Drawing.Bitmap
Dim pBitmapData As Imaging.BitmapData
Dim bytRgb() As Byte
Dim intLength As Integer
Dim mRect As Rectangle
'Get bitmap by specific file name
pBITMAP = System.Drawing.Bitmap.FromFile(strBmp)
mRect = New Rectangle(0, 0, pBITMAP.Width, pBITMAP.Height)
'Get bitmap data
pBitmapData = pBITMAP.LockBits(mRect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb)
'set array dimension, they array index will start from 0
intLength = 3 * pBITMAP.Height * pBITMAP.Width
ReDim bytRgb(intLength - 1)
'move memory from bitmap to the long array
Copy(pBitmapData.Scan0, bytRgb, 0, intLength)
pBITMAP.UnlockBits(pBitmapData)
'return value
Return bytRgb
'clean up
pBITMAP.Dispose()
mRect = Nothing
Erase bytRgb
Catch ex As Exception
Debug.Print(ex.Message)
Return Nothing
End Try
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Test bitmap
Dim aryByte() As Byte
Dim intIndx As Integer
aryByte = BytePixel("C:\Wylelabs\SampleCodes\VBNET\ByteTest\input\Test.bmp")
For intIndx = 0 To (aryByte.Length - 1) Step 3
Debug.Print(aryByte(intIndx) & "," & aryByte(intIndx + 1) & "," & aryByte(intIndx + 2))
If intIndx = 500 Then Stop
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|