使用VB.NET将bmp图像转换为二进制图像的代码

时间:2019-01-07 15:59:20

标签: vb.net

以下代码没有错误,该代码将灰度图像转换为二进制图像,但是它不起作用...我需要帮助以确定为什么该代码不起作用... I,m使用vb,net 2012

    Dim mp3 As Double
    mp2 = 0
    Dim i, j As Integer
    Dim img As New Bitmap(600, 600)
    ' PictureBox2.Cls()
    For i = 0 To w
        For j = 0 To h
            ab(i, j) = 0
            zxc(i, j) = 0
            zxc1(i, j) = 0
            ssc(i, j) = 0
        Next j

    Next i
    For i = 0 To w - 17 Step 16
        For j = 0 To h - 17 Step 16
            mp1 = 0
            For k = i To i + 15
                For l = j To j + 15
                    mp1 = mp1 + imageArray(k, l)
            Next l, k
            mp1 = mp1 / (16 ^ 2)
            For k = i To i + 15
                For l = j To j + 15
                    If imageArray(k, l) >= mp1 Then
                        ssc(k, l) = 255
                    Else
                        ssc(k, l) = 0
                    End If

                    img.SetPixel(k, l, Color.FromArgb(ssc(k, l), ssc(k, l), ssc(k, l)))
            Next l, k
        Next j

        PictureBox2.Refresh()
    Next i
    mp3 = 0 : nd = 1
    For k = 0 To w
        For l = j To h
            mp3 = mp3 + imageArray(k, l)
            nd = nd + 1
    Next l, k
    mp3 = mp3 / nd
    For k = 0 To w
        For l = j To h
            If imageArray(k, l) >= mp3 Then ssc(k, l) = 255 Else ssc(k, l) = 0
                            img.SetPixel(k, l, Color.FromArgb(ssc(k, l), ssc(k, l), ssc(k, l)))
    Next l, k
    mp3 = 0 : nd = 0
    For k = i To w
        For l = 0 To h
            mp3 = mp3 + imageArray(k, l)
            nd = nd + 1
    Next l, k
    mp3 = mp3 / nd
    For k = i To w
        For l = 0 To h
            If imageArray(k, l) >= mp3 Then ssc(k, l) = 255 Else ssc(k, l) = 0
                            img.SetPixel(k, l, Color.FromArgb(ssc(k, l), ssc(k, l), ssc(k, l)))

    Next l, k
    hpp = 0
    PictureBox2.Image = img

End Sub

........加载图像的代码正在运行,但是我不知道它是否正确存储了数组的值,如下所示

    Dim ofd As New OpenFileDialog
    ofd.Filter = "Bitmap files (*.bmp)|*.bmp"
    If ofd.ShowDialog = DialogResult.OK Then
        If ofd.FileName <> String.Empty Then
            Me.PictureBox1.Image = Bitmap.FromFile(ofd.FileName)
        End If
    End If
    Dim img As New Bitmap(ofd.FileName)
    Dim imageArray(img.Width, img.Height) As Integer
    Dim i, j As Integer
    Dim k As Double
    For i = 0 To img.Width - 1
        For j = 0 To img.Height - 1
            Dim pixel As Color = img.GetPixel(i, j)
            imageArray(i, j) = pixel.ToArgb()
            z(i, j) = imageArray(i, j)
            k = k + imageArray(i, j)
        Next j
    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

尝试此功能

Public Shared Function GetArrayFromImage(image As Image) As Byte()
    If image IsNot Nothing Then
        Dim ic As New ImageConverter()
        Dim buffer As Byte() = DirectCast(ic.ConvertTo(image, GetType(Byte())), Byte())
        Return buffer
    Else
        Return Nothing
    End If
End Function