/// <summary> /// Get bytes hidden in a picture's plane /// </summary> /// <param name="img">the picture to extract data from</param> /// <param name="planes">what pleanes to extract from</param> /// <param name="bitDepth">the bit depth to extract [1, 8]</param> /// <param name="bytesToGet">the number of bytes to retrieve</param> /// <returns>a byte array of data, null if there is a problem</returns> internal static byte[] ExtractBytes(Bitmap img, EmbeddingPlane planes, int bitDepth, int bytesToGet) { // var declatation byte[] retVal = new Byte[bytesToGet]; int currentByteIndex = 0; byte currentBitIndex = 1; try { // loop through the x and y pixels of the image, as well as the correct bit depths for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { for (byte extractBit = 1; extractBit <= bitDepth; extractBit++) { // extract from R if ((planes == EmbeddingPlane.R) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte r = SteganographyMethods.GetBitValueFromByte(rgb.R, extractBit); retVal[currentByteIndex] = SteganographyMethods.SetBitValueInByte(retVal[currentByteIndex], currentBitIndex, r); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= bytesToGet) { x = img.Width; y = img.Height; break; } } // extract from G if ((planes == EmbeddingPlane.G) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte g = SteganographyMethods.GetBitValueFromByte(rgb.G, extractBit); retVal[currentByteIndex] = SteganographyMethods.SetBitValueInByte(retVal[currentByteIndex], currentBitIndex, g); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= bytesToGet) { x = img.Width; y = img.Height; break; } } // extract from B if ((planes == EmbeddingPlane.B) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte b = SteganographyMethods.GetBitValueFromByte(rgb.B, extractBit); retVal[currentByteIndex] = SteganographyMethods.SetBitValueInByte(retVal[currentByteIndex], currentBitIndex, b); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= bytesToGet) { x = img.Width; y = img.Height; break; } } } } } } catch (Exception) { retVal = null; } return(retVal); }
/// <summary> /// Embeds content in the image /// </summary> /// <param name="img">the image in which to embed</param> /// <param name="planes">the planes to embed in</param> /// <param name="bitDepth">the bit depth of the embedding</param> /// <param name="content">the content to embed</param> /// <returns>true if successful</returns> public static Boolean Embed(Bitmap img, EmbeddingPlane planes, int bitDepth, EmbeddingContent content) { // short circuit if ((img == null) || (bitDepth < 1) || (bitDepth > 8) || (content == null) || (content.HeaderAndContent.Length > SteganographyMethods.MaxBytesInImage(img, planes, bitDepth))) { return(false); } // var init Boolean retVal = true; int currentByteIndex = 0; byte currentBitIndex = 1; try { // loop through the x and y pixels of the image, as well as the correct bit depths for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { for (byte embedBit = 1; embedBit <= bitDepth; embedBit++) { // embed in R if ((planes == EmbeddingPlane.R) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte r = SteganographyMethods.SetBitValueInByte(rgb.R, embedBit, SteganographyMethods.GetBitValueFromByte(content.HeaderAndContent[currentByteIndex], currentBitIndex)); img.SetPixel(x, y, Color.FromArgb(r, rgb.G, rgb.B)); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= content.HeaderAndContent.Length) { x = img.Width; y = img.Height; break; } } // embed in G if ((planes == EmbeddingPlane.G) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte g = SteganographyMethods.SetBitValueInByte(rgb.G, embedBit, SteganographyMethods.GetBitValueFromByte(content.HeaderAndContent[currentByteIndex], currentBitIndex)); img.SetPixel(x, y, Color.FromArgb(rgb.R, g, rgb.B)); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= content.HeaderAndContent.Length) { x = img.Width; y = img.Height; break; } } // embed in B if ((planes == EmbeddingPlane.B) || (planes == EmbeddingPlane.RGB)) { Color rgb = img.GetPixel(x, y); byte b = SteganographyMethods.SetBitValueInByte(rgb.B, embedBit, SteganographyMethods.GetBitValueFromByte(content.HeaderAndContent[currentByteIndex], currentBitIndex)); img.SetPixel(x, y, Color.FromArgb(rgb.R, rgb.G, b)); // enumerate bit/byte counter currentBitIndex++; if (currentBitIndex > 8) { currentBitIndex = 1; currentByteIndex++; } if (currentByteIndex >= content.HeaderAndContent.Length) { x = img.Width; y = img.Height; break; } } } } } } catch { retVal = false; } return(retVal); }