根据另外两个位图为新位图选择颜色

时间:2015-04-10 05:47:14

标签: .net arrays vb.net colors bitmap

我正在尝试创建一个基本的隐写程序。基本上我有一张图片上传到两个picture boxes。我使用它们创建了一个位图,并且可以通过单击以下代码来更改第二个picture box的颜色:

Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
        Dim MyColor As Color
        Dim bm As Bitmap = Me.PictureBox1.Image
        Dim bm2 As Bitmap = Me.PictureBox2.Image
        MyColor = bm.GetPixel(e.X, e.Y)
        Dim g As Graphics = PictureBox2.CreateGraphics
        bm2.SetPixel(e.X, e.Y, Color.Black)
        PictureBox2.Image = bm2
        MessageBox.Show( _
            "Pixel x=" & e.X & ", y=" & e.Y & ", color=" & MyColor.ToString, _
            "Color", _
            MessageBoxButtons.OK, _
            MessageBoxIcon.Information)

    End Sub

这可以改变第二个位图的像素。存储像素时,我使用以下代码:

Private Sub SaveToArray_Click(sender As Object, e As EventArgs) Handles SaveToArray.Click
        Dim bm As Bitmap = Me.PictureBox1.Image
        Dim bm2 As Bitmap = Me.PictureBox2.Image

        ReDim ColorArray(bm.Size.Height - 1, bm.Size.Width - 1)
        ReDim colorarray2(bm2.Size.Height - 1, bm2.Size.Width - 1)
        Dim i, j, k As Integer

        For i = 0 To bm.Size.Height - 1
            For j = 0 To bm.Size.Width - 1
                ColorArray(i, j) = bm.GetPixel(j, i)
                colorarray2(i, j) = bm2.GetPixel(j, i)
            Next
        Next
    End Sub

以上两个代码都运行正常。问题是当我想比较位图中的两个数组时。我想比较数组和每个相同颜色的像素,我希望像素颜色等于color.black为第三个数组。如果像素颜色不匹配,我希望第三个数组的像素颜色等于color.white。然后我想取第三个数组并绘制第三个位图。以下代码是我遇到的问题:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i, j As Integer
        Dim bm As Bitmap = Me.PictureBox1.Image
        Dim bm2 As Bitmap = Me.PictureBox2.Image

        For i = 0 To bm.Size.Height - 1
            For j = 0 To bm.Size.Width - 1
                If ColorArray(i, j) = colorarray2(i, j) Then

                    checkarray(i, j) = Color.Black
                Else
                    checkarray(i, j) = Color.White

                End If
            Next
        Next
        Dim bm3 As Bitmap
        For i = 0 To bm.Size.Height - 1
            For j = 0 To bm.Size.Width - 1
                bm3.SetPixel(i, j, checkarray(i, j))
            Next
        Next
        PictureBox2.Image = bm3
    End Sub

我明确地遇到了问题:

 For i = 0 To bm.Size.Height - 1
            For j = 0 To bm.Size.Width - 1
                If ColorArray(i, j) = colorarray2(i, j) Then

                    checkarray(i, j) = Color.Black
                Else
                    checkarray(i, j) = Color.White

                End If
            Next
        Next

我得到一个NullReferenceException Unhandled和Object引用未设置为对象的实例。

非常感谢任何帮助。谢谢!

编辑:

我将数组声明为:

Dim ColorArray(,) As Color
    Dim colorarray2(,) As Color
    Dim checkarray(,) As Color

2 个答案:

答案 0 :(得分:0)

您应该设置checkarray的大小:

ReDim checkarray(bm.Size.Height - 1, bm.Size.Width - 1)

你也会遇到" bm3"因为你还没有创建一个对象。将其更改为:

Dim bm3 As Bitmap = new Bitmap(bm.Size.Width, bm.Size.Height)


编辑:
你还必须切换i和j:

变化:

bm3.SetPixel(i, j, checkarray(i, j))

要:

bm3.SetPixel(j, i, checkarray(i, j))

答案 1 :(得分:0)

我认为通过首先在大多数情况下切换i和j以及将我的问题中的最后一段代码更改为此更容易:

Dim i, j As Integer

        Dim bm As Bitmap = Me.PictureBox1.Image
        Dim bm2 As Bitmap = Me.PictureBox2.Image
        Dim bm3 As Bitmap = Me.PictureBox2.Image
        ReDim checkarray(bm.Size.Width - 1, bm.Size.Height - 1)
        For i = 0 To bm.Size.Height - 1
            For j = 0 To bm.Size.Width - 1
                If ColorArray(j, i) = colorarray2(j, i) Then

                    bm3.SetPixel(j, i, Color.Black)
                Else
                    bm3.SetPixel(j, i, Color.White)

                End If
            Next
        Next
        PictureBox2.Image = bm3