0
votes

I am trying to create a basic steganography program. Basically I have one picture uploaded into two picture boxes. I create a bitmap with both of them and am able to change the color of the second picture box by clicking with the following code:

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

This works to change pixels of the second bit map. When storing the pixels I use this code:

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

Both of the above codes work fine. The problem is when I want to compare both arrays from the bitmaps. I want to compare the arrays and for each pixel that is the same color, I want the pixel color to equal color.black for a third array. If the pixel colors don't matchup I want the pixel color for the third array to equal color.white. Then I want to take that third array and draw a third bitmap. The following code is what I am having trouble on:

 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

I get the problem specifically at:

 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

This I get a NullReferenceException Unhandled and Object reference not set to an instance of an object.

Any help would be greatly appreciated. Thank you!

edit:

I declared the arrays as such:

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

2 Answers

0
votes

You should set the size of the checkarray:

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

Also you will have a problem with "bm3" as you haven't created an object yet. Change it to something like:

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


EDIT:
You also have to switch i and j:

Change:

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

To:

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

I figured it out as being much easier by first switching the i and j's throughout most of it and changing the last block of code in my question to this:

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