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.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using System.IO;
- namespace imgMessCons
- {
- class ImgProc
- {
- private Bitmap bmp;
- public ImgProc(string filePath)
- {
- this.bmp = new Bitmap(filePath);
- }
- public void WriteMessage(string filePath, int step)
- {
- int width = this.bmp.Width,
- height = this.bmp.Height,
- key = 0;
- char ch = '\0';
- string strMess = string.Empty;
- TextReader reader = new StreamReader(filePath, Encoding.Default);
- strMess = reader.ReadToEnd();
- reader.Close();
- TableTranslation tblTr = new TableTranslation();
- strMess = strMess + tblTr.GetSymbol(tblTr.GetLength() - 1).ToString();
- int lenStr = strMess.Length;
- int indexStrMess = 0,
- indexStrMess2 = 0;
- for (int i = 0; i < width; i++)
- {
- for (int j = 0; j < height; j++)
- {
- if ((indexStrMess + 1) % step == 0)
- {
- indexStrMess2 = (int)(indexStrMess / step);
- if (indexStrMess2 >= lenStr)
- {
- indexStrMess2 = indexStrMess2 % lenStr;
- }
- ch = strMess[indexStrMess2];
- key = tblTr.GetIndex(ch);
- this.bmp.SetPixel(i, j, Color.FromArgb(key, key, key));
- }
- if (indexStrMess == 0)
- {
- key = step;
- this.bmp.SetPixel(i, j, Color.FromArgb(key, key, key));
- }
- indexStrMess++;
- }
- }
- }
- public void ReadMessage(string filePath)
- {
- TableTranslation tbTr = new TableTranslation();
- int width = this.bmp.Width,
- heith = this.bmp.Height,
- step = 0,
- key = 0,
- finishKey = tbTr.GetLength() - 1;
- char ch = '\0';
- string strMess = string.Empty;
- Color clr;
- clr = this.bmp.GetPixel(0,0);
- step = (int)clr.R;
- int indexMess = 0;
- for (int i = 0; i < width; i++)
- {
- for (int j = 0; j < heith; j++)
- {
- if ((indexMess + 1) % step == 0)
- {
- clr = this.bmp.GetPixel(i, j);
- key = (int)clr.R;
- ch = tbTr.GetSymbol(key);
- strMess = strMess + ch.ToString();
- }
- if (key == finishKey)
- {
- i = width;
- j = heith;
- }
- indexMess++;
- }
- }
- strMess = strMess.Substring(0, strMess.Length - 1);
- StreamWriter sw = new StreamWriter(filePath);
- sw.Write(strMess);
- sw.Close();
- }
- public void SaveImg(string WriteImgPath)
- {
- string imgWithMessPath = Path.GetDirectoryName(WriteImgPath) + @"\imgMess.png";
- this.bmp.Save(imgWithMessPath);
- this.bmp = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment