4

Hey I am able to set the color of image via set Pixel Property but when i put condition getPixel then no error occurs but the programme stucks

i put the code below please check it give me the solution :

    Dim b As Bitmap = New Bitmap("D:\test.bmp")

' Make Image Indexed

        Dim nii As New Bitmap(b.Width, b.Height,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
        For y As Integer = 0 To nii.Height - 1
            For x = 0 To nii.Width - 1
                Dim cw As New Color
                cw = Color.Black
                If nii.GetPixel(x, y) = cw Then
                    nii.SetPixel(x, y, Red)
                End If

            Next
        Next
        PictureBox1.Image = FromFile("D:\test.bmp")
        PictureBox2.Image = nii

If i removed getPixel then programme works but full image color is going to be red.

Kartik Shah
  • 143
  • 2
  • 5
  • 14

2 Answers2

6

You need to compare the ARGB values of color

Dim cw As New Color
cw = Color.Black
dim curPixColor as Color = b.GetPixel(x, y)
If curPixColor.ToArgb = cw.ToArgb Then
    nii.SetPixel(x, y, Color.Red)
End If

Or you should use the Equality Operator

Dim cw As New Color
cw = Color.Black
dim curPixColor as Color = b.GetPixel(x, y)
If Color.op_Equality(curPixColor, cw) Then
    nii.SetPixel(x, y, Color.Red)
End If

Reference:http://msdn.microsoft.com/en-us/library/system.drawing.color.op_equality(v=vs.110).aspx

Edit: As you are getting pixel from a bmp there is no transparency supported. so your comparing color should be

cw = Color.FromArgb(0,0,0,0)

Edit2: you are reading pixed from nii you should be reading from b

dim curPixColor as Color = b.GetPixel(x, y)

full code should be something like (tested)

    Dim b As Bitmap = New Bitmap("D:\test.bmp")

    ' Make Image Indexed

    Dim nii As New Bitmap(b.Width, b.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
    For y As Integer = 0 To nii.Height - 1
        For x = 0 To nii.Width - 1
            Dim cw As New Color
            cw = Color.Black
            Dim curPixColor As Color = b.GetPixel(x, y)
            If curPixColor.ToArgb() = cw.ToArgb() Then
                nii.SetPixel(x, y, Color.Red)
            Else
                nii.SetPixel(x, y, curPixColor)
            End If
        Next
    Next
    PictureBox1.Image = Image.FromFile("D:\test.bmp")
    PictureBox2.Image = nii
bansi
  • 55,591
  • 6
  • 41
  • 52
  • i tried it but it dosen't work again i tried both solution !! :( – Kartik Shah Dec 02 '13 at 10:50
  • no error message nothing i got it continued its process and when debug i seen that control does not enter in if condition when condition becomes true.. – Kartik Shah Dec 02 '13 at 10:58
  • I see this in watch curPixColor.ToArgb = 0 and cw.ToArgb = -16777216 how the value is assaign ?? – Kartik Shah Dec 02 '13 at 11:15
  • hmm.. may be the color is not exact match. check the red, green and blue values. mainly check the alpha values. – bansi Dec 02 '13 at 11:17
  • i try this solution If curPixColor.A = cw.A And curPixColor.R = cw.R And curPixColor.G = cw.G And curPixColor.B = cw.B Then nii.SetPixel(x, y, Red) End If but still same problem – Kartik Shah Dec 02 '13 at 11:23
  • i change it and i got my full image going to red not the selected part or black part this is image [RUN-TIME] http://postimg.org/image/kerhjmztn/ – Kartik Shah Dec 02 '13 at 11:44
  • Yes its Done Thanks For your help..!! This is Change i made i put nii instead of b so i solve it thanks for your help..!! Dim curPixColor As Color = b.GetPixel(x, y) If curPixColor.ToArgb = cw.ToArgb Then nii.SetPixel(x, y, Red) End If – Kartik Shah Dec 02 '13 at 11:56
0

I had a similar problem, but comparing the RGB values worked. Thanks!

In fact, instead of too many variables, you can just do:

If b.GetPixel(x,y).ToArgb() = Color.Black.ToArgb() then......
Theo
  • 57,719
  • 8
  • 24
  • 41