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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20325986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:19:09  来源:igfitidea点击:

How do I get the color of a pixel at X,Y using vb.net

vb.netimagecolorsgetpixel

提问by Kartik Shah

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

嘿,我可以通过 set Pixel Property 设置图像的颜色,但是当我设置条件 getPixel 时,没有发生错误,但程序卡住了

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.

如果我删除了 getPixel,则程序可以运行,但完整的图像颜色将变为红色。

回答by bansi

You need to compare the ARGBvalues of color

您需要比较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

Or you should use the Equality Operator

或者你应该使用 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

参考: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

编辑:当您从 bmp 获取像素时,不支持透明度。所以你的比较颜色应该是

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

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

Edit2:你正在阅读nii你应该阅读的像素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

回答by Sugata Mitra

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

我有一个类似的问题,但比较 RGB 值有效。谢谢!

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

事实上,你可以这样做,而不是太多的变量:

If b.GetPixel(x,y).ToArgb() = Color.Black.ToArgb() then......