getting bitmap RGB values

C

chuck

I load a bitmap using GDI+
now, I want to collect all the pixel values, (in RGB but
without the A component) onto a color array, say...
dim btmp1(,) as color

is there a method that can help me collect the pixel values
as quickly as possible (like one row at a time)?
what about setting (or changing) the pixel values back onto
the bitmap?
 
R

Rigga

try bitnap.GetPixel

as in PixelColour = myBitmap.GetPixel(x,y)

and myBitmap.SetPixel(x,y,newcolour)

hopefully that should help you
 
J

Jeremy Cowles

chuck said:
I load a bitmap using GDI+
now, I want to collect all the pixel values, (in RGB but
without the A component) onto a color array, say...
dim btmp1(,) as color

is there a method that can help me collect the pixel values
as quickly as possible (like one row at a time)?
what about setting (or changing) the pixel values back onto
the bitmap?

Yes, it's not easy, but this way is the fastest (without using DX or some
outside tool). You can use the LockBitmapBits function, which returns the
first element in an array of bits. Then use the Marshal.Copy function to
copy the array based on the assumed length and the first element. It's
sequential, so the array ends up looking something like this:

'// each pixel has 3 elements in the array
'// (R, G, B - but this changes if you have an
'// Alpha channel)
Pixel1_RedValue = RgbValues(0)
Pixel1_BlueValue = RgbValues(1)
Pixel1_GreenValue = RgbValues(2)

'// next 3 elements are for the second pixel
Pixel2_RedValue = RgbValues(3)
Pixel2_BlueValue = RgbValues(4)
Pixel2_GreenValue = RgbValues(5)

I have attached a class that inverts an image using the LockBitmap function
(which *does* use an Alpha channel). It's pretty clean, and should give you
a good jump start.

HTH,
Jeremy
 
J

Jeremy Cowles

Rigga said:
try bitnap.GetPixel

as in PixelColour = myBitmap.GetPixel(x,y)

and myBitmap.SetPixel(x,y,newcolour)


This will work, but is incredibly slow.
 
C

chuck

I have attached a class that inverts an image using the LockBitmap function
(which *does* use an Alpha channel). It's pretty clean, and should give you
a good jump start.

awesome, but how do I get the attachment file :)
** this is most embarrassing..
 
S

scorpion53061

would you email this class to me please ?

(e-mail address removed)

remove nospamhere.
 
J

Jeremy Cowles

I have 3 hindsight notes on the Inverter Class:

1. The class returns a new instance of a bitmap on every call to Process( ),
this is slow and dumb, and defeats the purpose of working directly on the
image bits.

2. There is no reason for this class to implement IDisposable, or to hold
state (m_bmpImage) like it does. I added this for something else.

3. The entire class could be easily summed up into a single function that
takes a Bitmap as a parameter and operates directly on it, with no return
value.


For anyone who didn't get the attachment:
(watch for line breaks)
__________________________________________

Public Class Inverter
Implements IDisposable

Private m_bmpImage As Bitmap
Private Disposed As Boolean = False


Public Overloads Sub Dispose() Implements System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub

Protected Overrides Sub Finalize()
Me.Dispose(False)
MyBase.Finalize()
End Sub

Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If Not (Me.Disposed) Then
If (disposing) Then
'managed components
Me.Image.Dispose()
End If
'unmanaged
End If
Me.Disposed = True
End Sub


Public Property Image() As Bitmap
Get
Return m_bmpImage
End Get
Set(ByVal Value As Bitmap)
'// convert the image format
Dim g As Graphics
If Not Me.m_bmpImage Is Nothing Then
Me.m_bmpImage.Dispose()
End If

Me.m_bmpImage = New Bitmap(Value.Width, Value.Height,
Drawing.Imaging.PixelFormat.Format32bppRgb)

g = Graphics.FromImage(Me.m_bmpImage)
g.DrawImage(Value, 0, 0)
g.Dispose()

End Set
End Property

Public Function Process() As Image
Dim bts As Drawing.Imaging.BitmapData
Dim b() As Byte
Dim Y, iBDIndex, X As Integer

ReDim b((Image.Width * 4) * (Image.Height))

bts = Image.LockBits(New Rectangle(0, 0, Image.Width, Image.Height),
Drawing.Imaging.ImageLockMode.ReadWrite,
Drawing.Imaging.PixelFormat.Format32bppArgb)

Runtime.InteropServices.Marshal.Copy(bts.Scan0, b, 0, ((Image.Width)
* 4) * (Image.Height))

iBDIndex = 0

For Y = 0 To (Image.Height - 1)
For X = 0 To ((UBound(b) / 4) / (Image.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, ((Image.Width)
* 4) * (Image.Height))

Image.UnlockBits(bts)

Return New Bitmap(Image)
End Function
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top