嘿,我可以通过设置像素属性设置图像的颜色,但是当我把条件设置为getPixel时,没有错误发生但程序卡住了
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
如果我删除了getPixel,那么程序可以正常工作,但是完整的图像颜色会变成红色。
答案 0 :(得分:4)
您需要比较颜色的ARGB
值
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
或者你应该使用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
参考:http://msdn.microsoft.com/en-us/library/system.drawing.color.op_equality(v=vs.110).aspx
编辑: 当您从bmp获取像素时,不支持透明度。所以你的比较颜色应该是
cw = Color.FromArgb(0,0,0,0)
<强> EDIT2 强>:
你正在阅读来自nii
的像素,你应该从b
dim curPixColor as Color = b.GetPixel(x, y)
完整代码应该是(测试的)
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
答案 1 :(得分:0)
我有一个类似的问题,但是比较RGB值是可行的。谢谢!
实际上,您可以执行以下操作,而不是过多变量:
If b.GetPixel(x,y).ToArgb() = Color.Black.ToArgb() then......