Ejemplo n.º 1
0
 public bool CheckNowEmbedded()
 {
     if (embeddedState_ != EmbeddedState.Embedded)
     {
         return(false);
     }
     embeddedState_ = EmbeddedState.Handled;
     return(true);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Embed the text into the image.
        /// </summary>
        /// <param name="text">The text to embed.</param>
        /// <param name="bmp">The image to embed the text into.</param>
        /// <returns>The image containing the embedded text.</returns>
        public static Bitmap EmbedText(string text, Bitmap bmp)
        {
            // initially, we'll be hiding characters in the image
            EmbeddedState state = EmbeddedState.Hiding;

            // holds the index of the character that is being hidden
            int charIndex = 0;

            // holds the value of the character converted to integer
            int charValue = 0;

            // holds the index of the color element (R or G or B) that is currently being processed
            long pixelElementIndex = 0;

            // holds the number of trailing zeros that have been added when finishing the process
            int zeros = 0;

            // hold pixel elements
            int R = 0, G = 0, B = 0;

            // pass through the rows
            for (int i = 0; i < bmp.Height; i++)
            {
                // pass through each row
                for (int j = 0; j < bmp.Width; j++)
                {
                    // holds the pixel that is currently being processed
                    Color pixel = bmp.GetPixel(j, i);

                    // now, clear the least significant bit (LSB) from each pixel element
                    R = pixel.R - pixel.R % 2;
                    G = pixel.G - pixel.G % 2;
                    B = pixel.B - pixel.B % 2;

                    // for each pixel, pass through its elements (RGB)
                    for (int n = 0; n < 3; n++)
                    {
                        // check if new 8 bits has been processed
                        if (pixelElementIndex % 8 == 0)
                        {
                            // check if the whole process has finished
                            // we can say that it's finished when 8 zeros are added
                            if (state == EmbeddedState.Filling_With_Zeros && zeros == 8)
                            {
                                // apply the last pixel on the image
                                // even if only a part of its elements have been affected
                                if ((pixelElementIndex - 1) % 3 < 2)
                                {
                                    bmp.SetPixel(j, i, Color.FromArgb(R, G, B));
                                }

                                // return the bitmap with the text hidden in
                                return(bmp);
                            }

                            // check if all characters has been hidden
                            if (charIndex >= text.Length)
                            {
                                // start adding zeros to mark the end of the text
                                state = EmbeddedState.Filling_With_Zeros;
                            }
                            else
                            {
                                // move to the next character and process again
                                charValue = text[charIndex++];
                            }
                        }

                        // check which pixel element has the turn to hide a bit in its LSB
                        switch (pixelElementIndex % 3)
                        {
                        case 0:
                        {
                            if (state == EmbeddedState.Hiding)
                            {
                                // the rightmost bit in the character will be (charValue % 2)
                                // to put this value instead of the LSB of the pixel element
                                // just add it to it
                                // recall that the LSB of the pixel element had been cleared
                                // before this operation
                                R += charValue % 2;

                                // removes the added rightmost bit of the character
                                // such that next time we can reach the next one
                                charValue /= 2;
                            }
                        }
                        break;

                        case 1:
                        {
                            if (state == EmbeddedState.Hiding)
                            {
                                G += charValue % 2;

                                charValue /= 2;
                            }
                        }
                        break;

                        case 2:
                        {
                            if (state == EmbeddedState.Hiding)
                            {
                                B += charValue % 2;

                                charValue /= 2;
                            }

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

                        pixelElementIndex++;

                        if (state == EmbeddedState.Filling_With_Zeros)
                        {
                            // increment the value of zeros until it is 8
                            zeros++;
                        }
                    }
                }
            }

            return(bmp);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// The method is called in the host process to tell the Sandbox that the Window has embedded.
 /// </summary>
 public static void NowEmbedded()
 {
     embeddedState_ = EmbeddedState.Embedded;
 }