Use steganography to hide multiple images in C#


[hide multiple images]

The post Use steganography to hide one picture inside another in C# shows how you can hide one image inside another.

[hide multiple images]
This example shows one simple way to hide multiple images inside a single visible image. This program simply combines four smaller images into one image and then uses the previous post’s technique to hide the tiled image inside the visible image. Figure 1 shows the four images after they have been tiled into a single image.

The following code shows how the program tiles and hides the four images.

// Hide the four hidden images inside bm_visible
// and return the result.
public static Bitmap HideTiledImages(Bitmap bm_visible,
    Bitmap hidden1, Bitmap hidden2, Bitmap hidden3,
    Bitmap hidden4, int hidden_bits)
{
    // Tile the hidden images onto a
    // bitmap sized to fit the visible image.
    Bitmap bm = (Bitmap)bm_visible.Clone();
    int wid = bm.Width / 2;
    int hgt = bm.Height / 2;

    using (Graphics gr = Graphics.FromImage(bm))
    {
        Rectangle rect = new Rectangle(0, 0, wid, hgt);
        gr.DrawImage(hidden1, rect);

        rect.X += wid;
        gr.DrawImage(hidden2, rect);

        rect.X = 0;
        rect.Y += hgt;
        gr.DrawImage(hidden3, rect);

        rect.X += wid;
        gr.DrawImage(hidden4, rect);
    }

    // Hide the combined image in the main image.
    return HideImage(bm_visible, bm, hidden_bits);
}

The code divides the visible image’s width and height to calculate the width and height of the four smaller images. Note that this means the four smaller images must have the correct sizes. In my next post I’ll relax that restriction.

The program then makes a new bitmap that’s the same size as the visible image. It then uses the Graphics class’s DrawImage method to copy the smaller images onto the new bitmap. Finally it calls the HideImage method descriibed in the earlier post to hide the combined image.

The following code shows how the program retrieves four previously hidden images.

// Recover four hidden images.
public static void RecoverTiledImages(Bitmap bm_combined,
    out Bitmap hidden1, out Bitmap hidden2,
    out Bitmap hidden3, out Bitmap hidden4, int hidden_bits)
{
    // Recover the tiled image.
    Bitmap bm = RecoverImage(bm_combined, hidden_bits);

    // Pull out the pieces.
    int wid = bm_combined.Width / 2;
    int hgt = bm_combined.Height / 2;
    Rectangle dest = new Rectangle(0, 0, wid, hgt);

    Rectangle source = new Rectangle(0, 0, wid, hgt);
    hidden1 = new Bitmap(wid, hgt);
    using (Graphics gr = Graphics.FromImage(hidden1))
    {
        gr.DrawImage(bm, dest, source, GraphicsUnit.Pixel);
    }

    source.X += wid;
    hidden2 = new Bitmap(wid, hgt);
    using (Graphics gr = Graphics.FromImage(hidden2))
    {
        gr.DrawImage(bm, dest, source, GraphicsUnit.Pixel);
    }

    source.X = 0;
    source.Y += hgt;
    hidden3 = new Bitmap(wid, hgt);
    using (Graphics gr = Graphics.FromImage(hidden3))
    {
        gr.DrawImage(bm, dest, source, GraphicsUnit.Pixel);
    }

    source.X += wid;
    hidden4 = new Bitmap(wid, hgt);
    using (Graphics gr = Graphics.FromImage(hidden4))
    {
        gr.DrawImage(bm, dest, source, GraphicsUnit.Pixel);
    }
}

This method starts by using the RecoverImage method defined by the earlier post to recover the combined image shown in Figure 1. It calculates the size of the four sub-images and uses the Graphics class’s DrawImage method to recover the four smaller images.

The earlier post allows you to change the number of bits used for each of the visible and hidden images, so this example inherits that capability. Use the NumericUpDown control to determine the number of bits used for the hidden images.

As I mentioned earlier in this post, requiring the four hidden images to have exactly 1/4 the size of the original image is somewhat restrictive. In my next post, I’ll explain how you can avoid that restriction.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, cryptography, graphics, image processing, mathematics and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

8 Responses to Use steganography to hide multiple images in C#

  1. Pinku Singh says:

    What is the use of EncodeInMessage and pickposition function in this project

  2. Pinku Singh says:

    And also i did get the working of hide image method can you please explain

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.