Ciao
Il fatto è che nei data raw non vengono salvati in ordine (A) R G B, bensì B G R (A), quindi trovi il Blue in prima posizione, il Green sempre in seconda, Red in terza (e nel tuo caso basta, niente alpha), quindi la funzione andrebbe corretta da così
R(rgbValues(counter)) += 1
G(rgbValues(counter + 1)) += 1
B(rgbValues(counter + 2)) += 1
a così:
B(rgbValues(counter)) += 1
G(rgbValues(counter + 1)) += 1
R(rgbValues(counter + 2)) += 1
Comunque questo processo non è sempre corretto perché, per questioni di performance, alla fine di una "riga" di pixel, viene aggiunto del padding per allineare la memoria sui 4 byte, quindi se hai un'immagine con larghezza non multipla di 4, il risultato verrà sfasato.
In sostanza, una funzione di questo genere dovrebbe andare bene per i formati Format24bppRgb, Format32bppRgb e Format32bppArgb:
Public Function rgb(ByVal percorsoFile As String) As Int32()()
Dim R(255) As Int32
Dim G(255) As Int32
Dim B(255) As Int32
Dim RedValue As Int32
Dim GreenValue As Int32
Dim BlueValue As Int32
Dim mul, num As Integer
Dim immagine As New Bitmap(percorsoFile)
Dim rect As New Rectangle(0, 0, immagine.Width, immagine.Height)
Dim bmpData As BitmapData = immagine.LockBits(rect, _
ImageLockMode.ReadOnly, immagine.PixelFormat)
Dim ptr As IntPtr = bmpData.Scan0
Dim bytes As Integer = bmpData.Stride * bmpData.Height
Dim rgbValues(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
Select Case bmpData.PixelFormat
Case PixelFormat.Format24bppRgb
mul = 3
Case PixelFormat.Format32bppRgb
Case PixelFormat.Format32bppArgb
mul = 4
Case Else
Throw New NotSupportedException()
End Select
For y As Integer = 0 To bmpData.Height - 1
For x As Integer = 0 To bmpData.Width - 1
num = y * bmpData.Stride + mul * x
BlueValue = rgbValues(num)
GreenValue = rgbValues(num + 1)
RedValue = rgbValues(num + 2)
R(RedValue) += 1
G(GreenValue) += 1
B(BlueValue) += 1
Next
Next
immagine.UnlockBits(bmpData)
Return New Int32()() {R, G, B}
End Function
Tieni comunque presente che il formato dei pixel potrebbe anche essere diverso da questi, quindi dovresti scriverti delle funzioni personalizzate per la lettura di ogni possibile formato.
Luca