Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have problem with my image steganography code as it increases the size of the image after embeding the text.....for example if i load 10 kb pic as a input and embed 1 kb of text file then the resultent file size will be 150 kb aprox....thanks in advance

what is wrong with this piece of code???



C#
public static Bitmap embedText(string text, Bitmap bmp)
{
    State s = State.hiding;

    int charIndex = 0;
    int charValue = 0;
    long colorUnitIndex = 0;

    int zeros = 0;

    int R = 0, G = 0, B = 0;

    for (int i = 0; i < bmp.Height; i++)
    {
        for (int j = 0; j < bmp.Width; j++)
        {
            Color pixel = bmp.GetPixel(j, i);

            pixel = Color.FromArgb(pixel.R - pixel.R % 2,
                pixel.G - pixel.G % 2, pixel.B - pixel.B % 2);

            R = pixel.R; G = pixel.G; B = pixel.B;

            for (int n = 0; n < 3; n++)
            {
                if (colorUnitIndex % 8 == 0)
                {
                    if (zeros == 8)
                    {
                        if ((colorUnitIndex - 1) % 3 < 2)
                        {
                            bmp.SetPixel(j, i, Color.FromArgb(R, G, B));
                        }

                        return bmp;
                    }

                    if (charIndex >= text.Length)
                    {
                        s = State.filling_with_zeros;
                    }
                    else
                    {
                        charValue = text[charIndex++];
                    }
                }

                switch (colorUnitIndex % 3)
                {
                    case 0:
                        {
                            if (s == State.hiding)
                            {
                                R += charValue % 2;

                                charValue /= 2;
                            }
                        } break;
                    case 1:
                        {
                            if (s == State.hiding)
                            {
                                G += charValue % 2;

                                charValue /= 2;
                            }
                        } break;
                    case 2:
                        {
                            if (s == State.hiding)
                            {
                                B += charValue % 2;

                                charValue /= 2;
                            }

                            bmp.SetPixel(j, i, Color.FromArgb(R, G, B));
                        } break;
                }

                colorUnitIndex++;

                if (s == State.filling_with_zeros)
                {
                    zeros++;
                }
            }
        }
    }

    return bmp;
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 17-Jan-14 21:08pm
v2
Comments
BillWoodruff 18-Jan-14 2:17am    
Do you know for certain that the increase in file size is not what you should expect given the relative sizes of the source graphic file and the text to be hidden in it ?

Best guess?
You are loading a JPG file, and saving it as a bitmap...

A 10K bitmap is not big: it's roughly 50x50 pixels, so storing 1K of text into it isn't going to work too well anyway!
 
Share this answer
 
Comments
Adi5555 18-Jan-14 4:29am    
no it takes all types of image and save them in .png extention
OriginalGriff 18-Jan-14 5:08am    
PNG is a compressed format, but...it depends on the data how much compression you get because it is not a "lossy" format like JPG. So if you feed it a 10K JPG, add 1K text, and save as a PNG, then there is a very, very good chance that you will end up with a much bigger file!
Adi5555 19-Jan-14 8:12am    
Thanks ! i understand what u said ..... i wana know what should i do to maintain the size of the image after hiding data ???
OriginalGriff 19-Jan-14 8:24am    
The only way to do that is to use an uncompressed format: bitmap for example.
Input a bitmap : output a bitmap and they will be the same size.
Input a compressed format : output any format and it's unlikely they will be the same (or they will throw away some if not all of your hidden data when it is compressed)
Adi5555 19-Jan-14 8:31am    
but i have heard bitmap is not a good format security wise...is that true ??
The code used here is on a Romanian web site which seems to be accessible now only via the Google cached page: [^].

The same code is used on this web-site: [^], where the code shows a claim to Copyright by "Rajesh G." The code there also demonstrates the use of encryption and decryption of the Text used.

The OP here also has the same question on StackOverFlow: [^].

We have no direct evidence here that tells us what type of image file the OP is loading before applying the steganographic transformation. We don't know how many bits-per-pixel there are in the image file source, or how many bits-per-pixel are used in saving the transformed image. The code, obviously, shows we are dealing with at least 24 bpp (one byte each R G B). Seems reasonable to assume the image is being saved as a 24 bpp .png file (no indication the alpha channel is being used).

A cursory examination of the code suggests the processing is not adding any bytes/pixels to the image.

imho, the principle of Occam's Razor modulo "reasonable doubt" suggests we don't have enough information to make a reasonable guess, but I don't work for the NSA :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900