set image color

S

sympatico

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 AsNew Bitmap(img)
Dim eX AsInteger
Dim eY AsInteger
Dim r AsInteger
Dim g AsInteger
Dim b AsInteger
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))
EndIf
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.StretchIma ge
PeBox2.TabStop = False
Me.Controls.Add(PeBox2)
 
H

Herfried K. Wagner [MVP]

sympatico said:
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"

A simple (A)RGB image stores the color values of each pixel, in other words,
it's a rectangular matrix of (A)RGB values. In an indexed format, each
color used in the image is added to a color table that maps color indices to
actual color values. Instead of storing the RGB(A) values for each pixel,
the number of the pixel's color color in the color lookup table is stored.
When drawing the indexed image, for each pixel the actual color value of the
index stored for the pixel is taken from the loopup table (which is a
mapping of index -> (A)RGB).

What you need to do is converting the image from an indexed format to a
"raw" (A)RGB format. To do this, you will have to create a new bitmap
object with non-indexed pixel format, obtain a 'Graphics' object for it
('Graphics.FromImage') and paint the indexed bitmap onto this image
('Graphics.DrawImage'), for example.
 
L

Larry Serflaten

sympatico said:
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.

You might want to try a different approach (assume White is the new color)

Dim bmp As New Bitmap("d:\temp\test.bmp")
bmp.MakeTransparent(Color.Black)
PictureBox1.BackColor = Color.White
PictureBox1.Refresh()
PictureBox1.CreateGraphics.DrawImageUnscaled(bmp, 0, 0)

HTH
LFS
 

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