Пример #1
0
        /// <summary>
        /// Expected 471 length
        /// </summary>
        /// <returns></returns>
        public string ReadMessage()
        {
            var bmpPixelEnumerator = new BitmapPixelEnumerator(_img);
            var encodedLength = bmpPixelEnumerator.Take(PixelsWithMessageLengthAndPadding).ToArray();

            int indexLengthBit = 0;
            var ba = new BitArray(BitsWithMessageLengthAndPadding);
            foreach (var pixel in encodedLength)
            {
                indexLengthBit = ReadTwoBits(pixel.Blue, ba, indexLengthBit);
                indexLengthBit = ReadTwoBits(pixel.Green, ba, indexLengthBit);
                indexLengthBit = ReadTwoBits(pixel.Red, ba, indexLengthBit);
            }

            var messageLength = ba.ConvertBitsToWord();

            int indexMessageBit = 0;
            var messageBitArray = new BitArray(messageLength * PixelsWithMessageLengthAndPadding);
            bmpPixelEnumerator.Reset();
            var encodedPixels = bmpPixelEnumerator.Skip(PixelsWithMessageLengthAndPadding).Take((int)Math.Ceiling(messageLength * (double)PixelsWithMessageLengthAndPadding / (double)BitsPerPixel));
            foreach (var pixel in encodedPixels)
            {
                indexMessageBit = ReadTwoBits(pixel.Blue, messageBitArray, indexMessageBit);
                if (indexMessageBit >= messageBitArray.Count)
                {
                    break;
                }
                indexMessageBit = ReadTwoBits(pixel.Green, messageBitArray, indexMessageBit);
                if (indexMessageBit >= messageBitArray.Count)
                {
                    break;
                }
                indexMessageBit = ReadTwoBits(pixel.Red, messageBitArray, indexMessageBit);
                if (indexMessageBit >= messageBitArray.Count)
                {
                    break;
                }
            }

            byte[] messageBytes = new byte[messageLength];
            messageBitArray.CopyTo(messageBytes, 0);
            var message = Encoding.ASCII.GetString(messageBytes);

            return message;
        }