Go Back   CodeCall Programming Forum > Software Development > Tutorials > VB Tutorials
Register Blogs Search Today's Posts Mark Forums Read

VB Tutorials Visual Basic Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-2009, 10:09 AM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
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
__________________
My CC Blog | My Website |Periodic table | Pm me | Ask me
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-20-2009, 10:31 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-20-2009, 11:25 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Create Images with hidden messages

Very interesting! +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-30-2009, 04:09 PM
Newbie
 
Join Date: Oct 2009
Posts: 1
gordan is an unknown quantity at this point
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-30-2009, 04:15 PM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
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.
__________________
My CC Blog | My Website |Periodic table | Pm me | Ask me
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Storing Images in MySQL with PHP Jordan PHP Tutorials 43 01-16-2010 11:35 AM
Upgraded! Jordan Announcements 14 01-09-2009 03:40 PM
How do I make (files/folders) hidden option password protected? (WinXP) MPD Psycho Computer Software/OS 3 02-22-2008 11:05 AM
Optimizing Images DevilsCharm Search Engine Optimization 3 08-20-2006 10:36 AM


All times are GMT -5. The time now is 09:51 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0