+ Reply to Thread
Results 1 to 5 of 5

Thread: Create Images with hidden messages

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    Create Images with hidden messages

    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:

    Code:
    Imports System.IO



    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

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: Create Images with hidden messages

    Now that is cool! A new way to transfer messages!!!
    +rep and I'd do it twice if I could.

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: Create Images with hidden messages

    Very interesting! +rep
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Newbie gordan is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    1

    Re: Create Images with hidden messages

    Hi
    I just read your article and I have a simple question.
    Once you embed the secret image into the original one and then resize/crop that image
    will the secret message survive? Can it be extracted from the altered image?

    If possible would you please be so kind to send me the project file for the mention
    VB application. I can't seem to find it anywhere on your site.

    Thank you very much for your time.

  5. #5
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    Re: Create Images with hidden messages

    Then you have to crop/resize the original picture in the same way. The source code is in the first post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 08-13-2010, 04:42 PM
  2. Upgraded!
    By Jordan in forum Announcements
    Replies: 14
    Last Post: 01-09-2009, 12:40 PM
  3. Replies: 3
    Last Post: 02-22-2008, 08:05 AM
  4. Optimizing Images
    By DevilsCharm in forum Search Engine Optimization
    Replies: 3
    Last Post: 08-20-2006, 06:36 AM