Set Image color (plz help)

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
im trying to read an image(*.bmp) from file and then color the black pixels in it to the color i want using argb. Below is the code of what im trying to do but this gives me an error "setPixel is not supported for images with indexed pixel format"
Please tell me what im doing wrong.


Dim
c1 As Color = Color.Black
Dim c2 As Color = Color.FromArgb(CInt(colour))
Dim bm As New Bitmap(img)
Dim eX As Integer
Dim eY As Integer
Dim r As Integer
Dim g As Integer
Dim b As Integer
r = c2.R
g = c2.G
b = c2.B
For eX = 0 To bm.Width - 1
For eY = 0 To bm.Height - 1
If bm.GetPixel(eX, eY).ToArgb = c1.ToArgb() Then
bm.SetPixel(eX, eY, Color.FromArgb(r, g, b))
End If
Next eY
Next eX
PeBox2.Image = bm
PeBox2.Location =
New System.Drawing.Point(x, y)
PeBox2.Size = New System.Drawing.Size(width, height)
PeBox2.BorderStyle = BorderStyle.FixedSingle
PeBox2.Name = "PeBox2"
PeBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
PeBox2.TabStop = False
Me.Controls.Add(PeBox2)
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Ifyou just want to get rid of black why not use : bm.maketransparent(color.black)
(a bit less code... :) )

However if you want to stick with what you've got I'd say this should fix it :
dim bm as new bitmap (width, hight, imaging.pixelformat.dontcare)
bm.fromfile(img)

TPM
 
Top Bottom