public Dictionary<int, byte> Encode(byte[] message, string savePath) { List<byte> data = new List<byte>(); data.AddRange(BitConverter.GetBytes(message.Length)); data.AddRange(message); if (!MessageFitsImage(data)) { throw new ArgumentException("Message is too big to encode in the image."); } BinaryList bits = new BinaryList(data); if (InvertBits) bits.Invert(); LockedBitmap lockedBmp = new LockedBitmap(BitmapImage); lockedBmp.LockBits(); Dictionary<int, byte> changes = new Dictionary<int, byte>(); int bitIndex = 0; for (int pixelIndex = Seed[0]; pixelIndex <= (BitmapImage.Height * BitmapImage.Width) && bitIndex < bits.Count; pixelIndex += (Seed[bitIndex % Seed.Count] + 1)) { byte[] pixelValue = lockedBmp.GetPixelArgb(pixelIndex); BinaryOctet octet = pixelValue[(int)Color]; for (int currBit = 0; currBit < lsb; ++currBit) octet = octet.SetBit(currBit, bits[bitIndex++]); if (octet != pixelValue[(int)Color]) { changes.Add(pixelIndex, pixelValue[(int)Color]); pixelValue[(int)Color] = octet; } lockedBmp.SetPixel(PixelCoord(pixelIndex, BitmapImage.Width), pixelValue); } lockedBmp.UnlockBits(); BitmapImage.Save(savePath, ImageFormat.Png); return changes; }
private IEnumerable<byte> ReadBits(int byteCount) { BinaryList data = new BinaryList(); LockedBitmap lockedBmp = new LockedBitmap(BitmapImage); lockedBmp.LockBits(); int bitIndex = 0; for (int pixelIndex = Seed[0]; bitIndex < byteCount * 8; pixelIndex += (Seed[bitIndex % Seed.Count] + 1)) { BinaryOctet octet = lockedBmp.GetPixelArgb(pixelIndex)[(int)Color]; for (byte currBit = 0; currBit < lsb; ++currBit, ++bitIndex) data.Add(octet[currBit]); } lockedBmp.UnlockBits(); return data.ToBytes(InvertBits); }