Esempio n. 1
0
        /// <summary>
        /// Encodes a message into the loaded image.
        /// </summary>
        /// <param name="message"></param>
        public void EncodeMessage(string message, Stream outStream)
        {
            if (!IsImageLoaded)
            {
                throw new InvalidOperationException("No source image has been loaded.");
            }

            byte[] messageBytes = CharacterEncoding.GetBytes(message);

            if (messageBytes.Length > AvailableMessageBytes)
            {
                throw new InvalidOperationException(
                          string.Format("Unable to fit message. Message is {0} bytes, and only {1} bytes are available.",
                                        messageBytes.Length,
                                        AvailableMessageBytes)
                          );
            }

            MessagePosition     = 0;
            EndOfMessageReached = false;

            using (FastBitmap bitmap = new FastBitmap(SourceImage.Image))
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        WriteBytesToPixel(bitmap, messageBytes, x, y);

                        if (EndOfMessageReached)
                        {
                            break;
                        }
                    }


                    if (EndOfMessageReached)
                    {
                        break;
                    }
                }
            }


            SourceImage.Save(outStream);
        }