Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- namespace Image_Steganography
- {
- class ImageSteganoraphy
- {
- #region Variables
- private Bitmap convertedBitmapImage;
- private Bitmap encodedBitmapImage;
- #endregion
- #region Properties
- public Image OriginalImage { get; set; }
- public Bitmap BitmapImage { get{ return convertedBitmapImage;}}
- public Bitmap EncodedBitmapImage { get { return encodedBitmapImage; } }
- #endregion
- #region Constructor
- public ImageSteganoraphy(String sourcePath)
- {
- // 1)Get the original Image from a file path
- try
- {
- OriginalImage = Image.FromFile(sourcePath);
- }
- catch(Exception e)
- {
- throw new Exception(e.Message.ToString());
- }
- // 2)Convert the image to Bitmap
- convertedBitmapImage = new Bitmap(OriginalImage);
- }
- #endregion
- #region Methods
- private Color GetPixel(Bitmap imageToProcess, int x, int y)
- {
- return imageToProcess.GetPixel(x, y);
- }
- private int GetRedValue(Color pixel)
- {
- return pixel.R;
- }
- private int GetGreenValue(Color pixel)
- {
- return pixel.G;
- }
- private int GetBlueValue(Color pixel)
- {
- return pixel.B;
- }
- private Color CreateNewPixel(int redValue, int greenValue, int blueValue)
- {
- return Color.FromArgb(redValue, greenValue, blueValue);
- }
- private Color GetBitmapPixel(Bitmap imageToProcess, int x, int y)
- {
- return imageToProcess.GetPixel(x, y);
- }
- private void SetBitmapPixel(Bitmap imageToProcess, int x, int y,int redValue, int greenValue, int blueValue)
- {
- Color newPixel = CreateNewPixel(redValue, greenValue, blueValue);
- imageToProcess.SetPixel(x, y, newPixel);
- }
- private void SetBitmapPixel(Bitmap imageToProcess,int x, int y, Color pixel)
- {
- imageToProcess.SetPixel(x, y, pixel);
- }
- public void Encode(String encodeString, String fileName)
- {
- /* 1)Convert String to Hex
- * Example: "Hello" ->"48656C6C6F"
- *
- * */
- String hexString = HexMap.StringToHex(encodeString);
- /*2) Get row col pairs of each hex character from hex map
- * ----------- HEX MAP ------------
- * col 0 1 2 3
- * row 0 {'1','2','3','4'},
- row 1 {'5','6','7','8'},
- row 2 {'9','0','A','B'},
- row 3 {'C','D','E','F'}
- * */
- int[,] rowcol = HexMap.HexToRowCol(hexString);
- encodedBitmapImage = EncodeBitmap(convertedBitmapImage, rowcol);
- //Save bitmap
- encodedBitmapImage.Save(fileName);
- }
- private Bitmap EncodeBitmap(Bitmap imageToProcess, int[,] rowcol)
- {
- int rowcolIndex = 0;
- int width=0, height=0;
- //Encode all rowcol to bitmap
- for(width = 0; width<= imageToProcess.Width; width++)
- {
- for(height = 0; height <= imageToProcess.Height; height++)
- {
- Color pixelToEncode = GetBitmapPixel(imageToProcess, width, height);
- //ENCODING 1 HEX ROW COL PAIR TO RED & GREEN
- int redValue = GetRedValue(pixelToEncode);
- int newRedValue = ChangeLastDigit(rowcol[rowcolIndex, 0], redValue);
- int greenValue = GetGreenValue(pixelToEncode);
- int newGreenValue = ChangeLastDigit(rowcol[rowcolIndex, 1], greenValue);
- Color newPixel = Color.FromArgb(newRedValue, newGreenValue, GetBlueValue(pixelToEncode));
- SetBitmapPixel(imageToProcess, width, height, newPixel);
- rowcolIndex++;
- //Finished Encoding
- if (rowcolIndex == rowcol.Length/2)
- break;
- }
- //Finished Encoding
- if (rowcolIndex == rowcol.Length / 2)
- break;
- }
- //Encoding trailing bits to demark end of encoding
- //* Trailing bits have last digits of rgb value as 1, 2, 3, 4, 5, 6
- //* Example
- //* Pixel 1: Red 221 ->1
- //* Pixel 1: Green 122 ->2
- //* Pixel 1: Blue 003 ->3
- //* Pixel 2: Red 224 ->4
- //* Pixel 2: Green 135 ->5
- //* Pixel 2: Blue 216 ->6
- //* Hence i have used 2 pixels to denote end of encoding
- if (height + 1 >= imageToProcess.Height)
- {
- height = 0;
- width++;
- }
- else
- {
- height++;
- }
- Color _pixelToEncode = GetBitmapPixel(imageToProcess, width, height);
- int red = _pixelToEncode.R;
- int newRed = ChangeLastDigit(1, red);
- int green = _pixelToEncode.G;
- int newGreen = ChangeLastDigit(2, green);
- int blue = _pixelToEncode.B;
- int newBlue = ChangeLastDigit(3, blue);
- Color _newPixel = Color.FromArgb(newRed, newGreen, newBlue);
- SetBitmapPixel(imageToProcess, width, height, _newPixel);
- if (height + 1 >= imageToProcess.Height)
- {
- height = 0;
- width++;
- }
- else
- {
- height++;
- }
- _pixelToEncode = GetBitmapPixel(imageToProcess, width, height);
- red = _pixelToEncode.R;
- newRed = ChangeLastDigit(4, red);
- green = _pixelToEncode.G;
- newGreen = ChangeLastDigit(5, green);
- blue = _pixelToEncode.B;
- newBlue = ChangeLastDigit(5, blue);
- _newPixel = Color.FromArgb(newRed, newGreen, newBlue);
- SetBitmapPixel(imageToProcess, width, height, _newPixel);
- //Save bitmap
- return imageToProcess;
- }
- private bool IsTrailingPixel(Bitmap img, int row, int col)
- {
- Color pixel_1 = GetBitmapPixel(img, row, col);
- int red, blue, green;
- red = GetRedValue(pixel_1);
- green = GetGreenValue(pixel_1);
- blue = GetBlueValue(pixel_1);
- //1, 2, 3 found, next find 4, 5, 6
- if(GetLastDigit(red) == 1 && GetLastDigit(green) == 2 && GetLastDigit(blue) == 3)
- {
- if (col + 1 >= img.Height)
- {
- col = 0;
- row++;
- }
- else
- {
- col++;
- }
- Color pixel_2 = GetBitmapPixel(img, row, col);
- red = GetRedValue(pixel_2);
- green = GetGreenValue(pixel_2);
- blue = GetBlueValue(pixel_2);
- if(GetLastDigit(red) == 4 && GetLastDigit(green) == 5 && GetLastDigit(blue) == 5)
- {
- return true;
- }
- }
- return false;
- }
- public String Decode(String imgPath)
- {
- int width=0, height=0;
- String decodedHex = String.Empty;
- Bitmap img = new Bitmap(imgPath);
- //Decode all rowcol to bitmap
- for (width = 0; width <= img.Width; width++)
- {
- for (height = 0; height <= img.Height; height++)
- {
- if(IsTrailingPixel(img,width,height))
- {
- return HexMap.HexToString(decodedHex);
- }
- Color pixel = img.GetPixel(width, height);
- int row = GetLastDigit(GetRedValue(pixel));
- int col = GetLastDigit(GetGreenValue(pixel));
- decodedHex += HexMap.RCTOHEX(row, col);
- }
- }
- return HexMap.HexToString(decodedHex);
- }
- private int ChangeLastDigit(int digit, int value)
- {
- return value / 10 * 10 + digit;
- }
- private int GetLastDigit(int value)
- {
- return value % 10;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement