1

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???

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;
}
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
  • What file type do you load it from and save it as? If you actually save it as a `bmp`, or a `jpg`, `png`, whatever with different settings, the file size will definitely be much larger. – Dave Zych Jan 17 '14 at 16:38
  • NOt my normal domain, so disregard if I'm out of my element, but if you're adding text to an image, you shouldn't expect the resultant file size to reflect the number of bytes in a text string that represents the text you're adding. A graphical representation of a string will almost assuredly be larger than the sequence of bytes making up the string. – Brian Warshaw Jan 17 '14 at 16:39
  • 3
    Can you clarify your question of "what is wrong with this code"? this seems separate from the question of "Why does my file size get larger"? Steganography effectively adds a kind of noise (even if it is meaningful noise) to the image which will make all compression techniques harder so you would expect a bigger file. How much bigger depends on the file format used. – Chris Jan 17 '14 at 16:39
  • Maybe your steganography breaks the image encoding. Try using bitmap, which basically has no encoding, then the extra size won't be so obvious. – MrFox Jan 17 '14 at 16:39
  • I concur with what Dave said about different image types. Quick test, load your original image and save it without modifying it. – Reti43 Jan 17 '14 at 17:46
  • Dave Zych sir it takes .png or jpg or bmp ext image and the resultent image is in bit map extention......what should i change with this code ...should i only allow bmp extention image as a input ?? – Muhammad Adil Jan 18 '14 at 05:22

1 Answers1

0

Regardless of the input type extension, the image is converted to RGB for pixel manipulation. Since you directly change the pixels, what you want is to save the image with a lossless type, such as bitmap or png. Bitmap is uncompressed, so it takes up more space.

Reti43
  • 9,656
  • 3
  • 28
  • 44
  • You don't have to. PNG is lossless compressed bitmap (RGB) images. Compression reduces the redundancy in the data and lossless means you can retrieve the original data without any loss of information. In comparison, jpeg is lossy. [This post](http://stackoverflow.com/questions/2336522/png-vs-gif-vs-jpeg-when-best-to-use/7752936#7752936) explains it very nicely. – Reti43 Jan 18 '14 at 16:46