如何使用vb.net将彩色图像转换为黑白图像?

时间:2016-07-03 09:44:58

标签: vb.net image grayscale

我使用Visual Studio 10.i想要将彩色图像转换为黑白图像吗? 我试图做以下代码它获得成功,但它需要很多时间来转换。我可以在vb.net中不使用anyloop进行转换。 在此先感谢

Try
                        Dim img As Bitmap = New Bitmap((Image.FromFile("D:\\imgnam.jpg")))
                        Dim c As Color
                        Dim i As Integer = 0
                        Do While (i < img.Width)
                            Dim j As Integer = 0
                            Do While (j < img.Height)
                                c = img.GetPixel(i, j)
                                Dim r As Integer = 0
                                r = Convert.ToInt16(c.R)
                                Dim g As Integer = 0
                                g = Convert.ToInt16(c.G)
                                Dim b As Integer = 0
                                b = Convert.ToInt16(c.B)
                                Dim ans As Integer = ((r _
                                            + (g + b)) _
                                            / 3)
                                If (ans > 128) Then
                                    r = 255
                                    g = 255
                                    b = 255
                                Else
                                    r = 0
                                    g = 0
                                    b = 0
                                End If
                                c = Color.FromArgb(r, g, b)
                                img.SetPixel(i, j, c)
                                j = (j + 1)
                            Loop
                            i = (i + 1)
                        Loop

                        img.Save("D:\\imgnamNew.jpg")
                        Dim Bmp1 As New Image(Of Bgr, [Byte])("D:\\imgnamNew.jpg")
                        ImageFrame = Bmp1

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

0 个答案:

没有答案