gpt4 book ai didi

vb.net - 如何使用 vb.net 获取 X、Y 处像素的颜色

转载 作者:行者123 更新时间:2023-12-04 00:39:44 36 4
gpt4 key购买 nike

嘿,我可以通过 set Pixel Property 设置图像的颜色,但是当我输入条件 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,则程序可以运行,但整个图像颜色将变为红色。

最佳答案

你需要比较颜色的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

或者你应该使用相等运算符

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)

编辑2:你正在从 nii 阅读 pixed 你应该从 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

关于vb.net - 如何使用 vb.net 获取 X、Y 处像素的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20325986/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com