Hello and welcome to my tutorial were I'm going to show how you can store a secret message in a image.
First we need to add 4 Items:
First add a menustrip the name isn't so important, name it something good. Then add a menu in the menustrip called Images and in that menu you add 4 menus: Open, Save, Extract and Combine.
Then you add a picturebox called pctSecretImage and set dock to fill.
Add an OpenFileDialog called ofdImages and a SaveFileDialog called sfdImages.
Now it could look like this:

Now when that is done we go to the code, first an import:
We create three handlers and add a little bit of code:
Code:
Private Sub AddImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddImageToolStripMenuItem.Click
ofdImages.Title = "Open"
ofdImages.ShowDialog()
End Sub
Private Sub ExtractImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExtractToolStripMenuItem.Click
ofdImages.Title = "Extract"
ofdImages.ShowDialog()
End Sub
Private Sub CombineImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CombineToolStripMenuItem.Click
ofdImages.Title = "Combine"
ofdImages.ShowDialog()
End Sub
This will make so if the user click one of these three menus the OpenFileDialog will open and change title to what it does.
When the OpenFileDialog closes we want something to happened, so the we add this:
Code:
Private Sub ofdImages_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdImages.FileOk
If ofdImages.Title = "Open" Then
Try
Dim cImage As Bitmap = Bitmap.FromFile(ofdImages.FileName)
Dim newImage As New Bitmap(cImage.Width, cImage.Height)
For counterX As Integer = 0 To cImage.Width - 1
For counterY As Integer = 0 To cImage.Height - 1
newImage.SetPixel(counterX, counterY, cImage.GetPixel(counterX, counterY))
Next
Next
pctSecretImage.Image = newImage
Catch
MessageBox.Show("Error")
End Try
If the title of the ofdImages was Open (the user clicked the Open button) we stores the opened picture in a variable, then we go through all pixels and add them to a new one. This is just because we need to use this system later and then we need to do it now so the pictures will change the same way. And then we add the image to the picturebox.
But if the user clicked "Combine":
Code:
ElseIf ofdImages.Title = "Combine" Then
Try
Dim cImage As Bitmap = Bitmap.FromFile(ofdImages.FileName)
Dim FirstColor As Color
Dim NewImage As Bitmap = pctSecretImage.Image
FirstColor = cImage.GetPixel(0, 0)
For counterX As Integer = 0 To cImage.Width - 1
For counterY As Integer = 0 To cImage.Height - 1
If cImage.GetPixel(counterX, counterY) <> FirstColor Then
Dim AddColor As Color = NewImage.GetPixel(counterX, counterY)
Dim newR As Integer
Dim newG As Integer
Dim newB As Integer
If AddColor.R = 255 Then
newR = 254
Else
newR = AddColor.R + 1
End If
If AddColor.G = 255 Then
newG = 254
Else
newG = AddColor.G + 1
End If
If AddColor.B = 255 Then
newB = 254
Else
newB = AddColor.B + 1
End If
AddColor = Color.FromArgb(newR, newG, newB)
NewImage.SetPixel(counterX, counterY, AddColor)
End If
Next
Next
pctSecretImage.Image = NewImage
Catch
MessageBox.Show("Error")
End Try
So here we store our image as one variable and the image in the picturebox in another. The we stores the color on coordinate (0,0) in the loaded image as a third variable.
Now we're going through all the pixels in our opened picture. And if that pixel hasn't got the same color as our colorvariable we want to modify the image from the picturebox. The same pixels in that image will change color by 1r,1g and 1b. This won't be visible for the human eye. And then we add this new image to the picturebox.
Now we need to create a way to extract the secret message from the image:
Code:
ElseIf ofdImages.Title = "Extract" Then
Try
Dim cImage As Bitmap = Bitmap.FromFile(ofdImages.FileName)
Dim OldImage As Bitmap = pctSecretImage.Image
Dim NewImage As New Bitmap(OldImage.Width, OldImage.Height)
For counterX As Integer = 0 To cImage.Width - 1
For counterY As Integer = 0 To cImage.Height - 1
If cImage.GetPixel(counterX, counterY) <> OldImage.GetPixel(counterX, counterY) Then
NewImage.SetPixel(counterX, counterY, Color.Black)
Else
NewImage.SetPixel(counterX, counterY, Color.White)
End If
Next
Next
pctSecretImage.Image = NewImage
Catch
MessageBox.Show("Error")
End Try
End If
End Sub
Now the image has been open it's stored in one variable then we add the image of the picturebox to another and also declares a new image.
Then we're going through all the pixels, if the first image's and the second image's pixel had the same color our new image's pixel in the same location will be white, else it will be black.
Now we add 2 new subs for the saving:
Code:
Private Sub SaveImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveImageToolStripMenuItem.Click
sfdImages.ShowDialog()
End Sub
and...
Code:
Private Sub SaveImage_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveImage.FileOk
Dim SaveThisImage As Bitmap = pctSecretImage.Image
SaveThisImage.Save(sfdImages.FileName)
End Sub
This will simply save the image in the picturebox.
And now we're done, but before this tutorial ends I will of course show how it's working:
Start the program.
Press open and select an image, I select this: (It's nice isn't it?
)

Then press combine and select an image with your secret message. OBSERVE!!!!!! This image can only have 2 colors, one for the text and one for the background.
Here's my message:

Now the message have been added to the picture even if you can't see it.
So then you should press save and save it.
My picture now looks like this, you can't see the text at all:

Now you can close the program if you want and open the picture later, but since we already have the picture in the picturebox this isn't necessary.
To Extract the message you first need to have the image with the secret message in (even though you can't see it) in the picturebox and then press extract. Now you need to select the picture you had in the beginning and the program will compare the two pictures and it will find the difference.
Here's my result:

I've tested it with a white background but I couldn't see the message even though I didn't have any pictures in the way, the difference is so small.
Bye
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum