473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

getting bitmap RGB values

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?


Nov 20 '05 #1
7 6079
try bitnap.GetPixel

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

and myBitmap.SetPixel(x,y,newcolour)

hopefully that should help you
"chuck" <an*******@discussions.microsoft.com> wrote in message
news:0b****************************@phx.gbl...
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?

Nov 20 '05 #2
"chuck" <an*******@discussions.microsoft.com> wrote in message
news:0b****************************@phx.gbl...
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

Nov 20 '05 #3
"Rigga" <s@g.c> wrote in message
news:cJ********************@wards.force9.net...
try bitnap.GetPixel

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

and myBitmap.SetPixel(x,y,newcolour)

This will work, but is incredibly slow.

Nov 20 '05 #4
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 youa good jump start.


awesome, but how do I get the attachment file :-)
** this is most embarrassing..
Nov 20 '05 #5
oh, sorry, I wasn't thinking..
i see it.

Nov 20 '05 #6
would you email this class to me please ?

kj***********@charter.net

remove nospamhere.

"chuck" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
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..

Nov 20 '05 #7
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

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Amey | last post by:
Hi all I have an ARGB value for a Colour (Eg. -65536. The value was retrieved by using the Color.ToArgb method), is there any way that I can create a System.Drawing.Image or a...
2
by: James Dean | last post by:
I create a bitmap like this. The width and height are got from the compressed file i am reading. The width and height are in pixels.....1bpp bitmap = new...
3
by: Mats Boberg | last post by:
Hi, I have problems with saving a bitmap to hdd from my asp.net page I get the following error: "A generic error occurred in GDI+." Code: Bitmap bmp = new Bitmap(240,120);
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
5
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color...
3
by: Dave | last post by:
Hey all, I have a problem and I can't find anything on the net to really address it. We have a program at work that has lots of numbers on the front of the form because it is a control program...
0
by: bigsley | last post by:
Hi all. I am currently working on a Form project using C# and visual studio 2005. My UI has a large screen area on which the user can put text boxes and other controls. My UI also has a "global...
6
by: herzog | last post by:
Hi, I am using C# to a draw a graphic on a memory bitmap. I then extract the color information of every pixel of the bitmap. I do this with Bitmap.GetPixel(). The trouble I'm having is that I...
2
by: hgarg | last post by:
The onpaint() is getting called before calculating the required parameters by listBox1_SelectedIndexChanged function. I tried calling it at the end of this function. But of no use. Due to this issue...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.