"Harry" <len00x@gmx.net> wrote in message
news:1063709090.968958@news.aic.at...[color=blue]
> Hello,
>
> I am using a 2-dimensional matrix for image manipulation and recognition.
> The x-axes is image.width pixels long and the y-axes image.height.
> All fields have a RGB integer value.
> To store this value from the image into the matrix I am using the
> bitmap.getPixel(x,y) method because manipulating values in the matrix is
> much faster than using the bitmap.setPixel(x,y,color) method.
> My problem is, that the getPixel function is also very slow.
> Is there a way to get the RGB values from the picture more easily?[/color]
This is really a question for the dotnet.Framework.Drawing group, but I
might know a way to speed things up; Have you tried using the LockBits( )
function on the bitmaps? It will return a 1 dimensional array of values in
the current bitmap format. The array looks like this (depending on the
bitmap format):
{r,g,b,r,g,b,r,g,b,r,g,b}
By reading the properties of the BitmapData structure, you can determine how
to loop through the structure so you get one pixel each pass (stepping).
Here is some code I was playing with to invert an image (it is hard-coded
for 32bpp ARGB, and will convert your buffer image to this format
[bmpBuffer]):
'-------------------------------8<----------------------------
'// warning: this is not optimised at all!
'
Sub Invert(bmpBuffer as Bitmap)
Dim bts As Drawing.Imaging.BitmapData
Dim b() As Byte
Dim Y, iBDIndex, X As Integer
Dim bmTemp As New Bitmap(bmpBuffer.Width, bmpBuffer.Height, _
Drawing.Imaging.PixelFormat.Format32bppRgb)
Dim g As Graphics = Graphics.FromImage(bmTemp)
ReDim b((bmpBuffer.Width * 4) * (bmpBuffer.Height))
g.DrawImage(bmpBuffer, 0, 0)
g.Dispose()
bts = bmTemp.LockBits(New Rectangle(0, 0, bmpBuffer.Width, _
bmpBuffer.Height), Drawing.Imaging.ImageLockMode.ReadWrite, _
Drawing.Imaging.PixelFormat.Format32bppArgb)
Runtime.InteropServices.Marshal.Copy(bts.Scan0, b, 0,
((bmpBuffer.Width) * 4) _
* (bmpBuffer.Height))
iBDIndex = 0
'// divides by 4, and steps by 4 because it is ARGB 32bpp
For Y = 0 To (bmpBuffer.Height - 1)
For X = 0 To ((UBound(b) / 4) / (bmpBuffer.Height)) - 1
b(iBDIndex) = Not b(iBDIndex)
b(iBDIndex + 1) = Not b(iBDIndex + 1)
b(iBDIndex + 2) = Not b(iBDIndex + 2)
iBDIndex += 4
Next
Next
Runtime.InteropServices.Marshal.Copy(b, 0, bts.Scan0, _
((bmpBuffer.Width) * 4) * (bmpBuffer.Height))
bmTemp.UnlockBits(bts)
picMain.Image.Dispose()
bmpBuffer.Dispose()
bmpBuffer = New Bitmap(bmTemp)
bmTemp.Dispose()
End Sub
'-------------------------------8<----------------------------
A good site for drawing is
http://www.vbaccelerator.com/ and the Drawing
newsgroup (microsoft.public.dotnet.framework.drawing).
HTH,
Jeremy