0

I am able to hide my text inside an Image. But when I tried to extract the text from my image, I always only able manage to get only the first character. I don't know where went wrong?

My embed operation code:

public static Bitmap embedMessage(string hiddenText, Bitmap oriImage)
        {
            Color currentPixel;
            int[] colorRGB = new int[3];
            List<int> messageValue = new List<int>();
            messageValue = ConvertMessage(messageValue, hiddenText);
            int messageIndex = messageValue.Count;
            int binaryCount = 0;
            int zeroAdded = 0;


            for(int row = 0; row < oriImage.Height; row++)
            {
                for(int col = 0; col < oriImage.Width; col++)
                {
                    currentPixel = oriImage.GetPixel(col, row);

                    colorRGB[0] = ConvertEven(currentPixel.R);
                    colorRGB[1] = ConvertEven(currentPixel.G);
                    colorRGB[2] = ConvertEven(currentPixel.B);

                    for(int rgbIndex = 0; rgbIndex < colorRGB.Length; rgbIndex++)
                    {
                        if(messageIndex > 0)
                        {
                            colorRGB[rgbIndex] += messageValue[messageValue.Count - messageIndex] % 2;
                            messageValue[messageValue.Count - messageIndex] /= 2;
                        }
                    }

                    if (messageIndex == 0 && zeroAdded < 8)
                    {
                        oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2]));
                        zeroAdded++;
                        Console.WriteLine("Adding zero number: " + zeroAdded);
                    }
                    else
                    {
                        Console.WriteLine("Set Pixel");
                        oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2]));
                    }

                    if (zeroAdded == 8)
                    {
                        Console.WriteLine("Final Pixel Add");
                        oriImage.SetPixel(col, row, Color.FromArgb(colorRGB[0], colorRGB[1], colorRGB[2]));
                        return oriImage;
                    }

                    binaryCount++;
                    if (binaryCount % 8 == 0) { messageIndex--; Console.WriteLine("Message Index deducted"); }

                }
            }
            return oriImage;
        }

My embed implementation is the same to this example As for extraction I used the exact extraction code from the example. No matter what I tried, I am still only getting the first character of my embedded text. I tried checking my code by printing each operations and all of them fires without any issue which means the embed operation should be working as expected.

Winter Leong
  • 95
  • 1
  • 1
  • 7
  • 2
    Take a debugger and check in runtime that every variable has the expected value at any moment. – zerkms Jun 17 '17 at 09:01
  • @zerkms Thanks for the debugging suggestion! Just tried printing the R G B variables and my version definitely is not working as what I expected. Definitely something wrong with my embedding part. – Winter Leong Jun 17 '17 at 09:18

1 Answers1

0

Just found my problem. Everything in my embed method should be in the R G B for loop.

Winter Leong
  • 95
  • 1
  • 1
  • 7
  • As it stands, I can see you're using the `.SetPixel` method, which is known to be slow: (https://stackoverflow.com/questions/7768711/setpixel-is-too-slow-is-there-a-faster-way-to-draw-to-bitmap); I actually wrote a similar program a while back to store compressed files inside images to share in GitHub images (back before they had arbitrary upload). https://github.com/ckpearson/Pack/blob/1.1/Pak/Packer.cs#L139 <- This part shows how I write the data into the image pixel-by-pixel in a fast way. – Clint Jun 17 '17 at 11:11