Guest User

ImgProc.cs

a guest
Jul 14th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace imgMessCons
  12. {
  13. class ImgProc
  14. {
  15. private Bitmap bmp;
  16.  
  17. public ImgProc(string filePath)
  18. {
  19. this.bmp = new Bitmap(filePath);
  20. }
  21.  
  22. public void WriteMessage(string filePath, int step)
  23. {
  24. int width = this.bmp.Width,
  25. height = this.bmp.Height,
  26. key = 0;
  27.  
  28. char ch = '\0';
  29.  
  30. string strMess = string.Empty;
  31.  
  32. TextReader reader = new StreamReader(filePath, Encoding.Default);
  33.  
  34. strMess = reader.ReadToEnd();
  35.  
  36. reader.Close();
  37.  
  38. TableTranslation tblTr = new TableTranslation();
  39.  
  40. strMess = strMess + tblTr.GetSymbol(tblTr.GetLength() - 1).ToString();
  41.  
  42. int lenStr = strMess.Length;
  43.  
  44. int indexStrMess = 0,
  45. indexStrMess2 = 0;
  46.  
  47.  
  48. for (int i = 0; i < width; i++)
  49. {
  50. for (int j = 0; j < height; j++)
  51. {
  52. if ((indexStrMess + 1) % step == 0)
  53. {
  54. indexStrMess2 = (int)(indexStrMess / step);
  55. if (indexStrMess2 >= lenStr)
  56. {
  57. indexStrMess2 = indexStrMess2 % lenStr;
  58. }
  59.  
  60. ch = strMess[indexStrMess2];
  61.  
  62. key = tblTr.GetIndex(ch);
  63.  
  64. this.bmp.SetPixel(i, j, Color.FromArgb(key, key, key));
  65. }
  66.  
  67. if (indexStrMess == 0)
  68. {
  69. key = step;
  70.  
  71. this.bmp.SetPixel(i, j, Color.FromArgb(key, key, key));
  72. }
  73.  
  74. indexStrMess++;
  75. }
  76.  
  77. }
  78.  
  79.  
  80. }
  81.  
  82. public void ReadMessage(string filePath)
  83. {
  84. TableTranslation tbTr = new TableTranslation();
  85.  
  86. int width = this.bmp.Width,
  87. heith = this.bmp.Height,
  88. step = 0,
  89. key = 0,
  90. finishKey = tbTr.GetLength() - 1;
  91.  
  92. char ch = '\0';
  93.  
  94. string strMess = string.Empty;
  95.  
  96. Color clr;
  97.  
  98. clr = this.bmp.GetPixel(0,0);
  99.  
  100. step = (int)clr.R;
  101.  
  102. int indexMess = 0;
  103.  
  104. for (int i = 0; i < width; i++)
  105. {
  106. for (int j = 0; j < heith; j++)
  107. {
  108. if ((indexMess + 1) % step == 0)
  109. {
  110. clr = this.bmp.GetPixel(i, j);
  111.  
  112. key = (int)clr.R;
  113.  
  114. ch = tbTr.GetSymbol(key);
  115.  
  116. strMess = strMess + ch.ToString();
  117. }
  118.  
  119. if (key == finishKey)
  120. {
  121. i = width;
  122. j = heith;
  123. }
  124.  
  125. indexMess++;
  126. }
  127. }
  128.  
  129.  
  130. strMess = strMess.Substring(0, strMess.Length - 1);
  131. StreamWriter sw = new StreamWriter(filePath);
  132.  
  133. sw.Write(strMess);
  134.  
  135. sw.Close();
  136.  
  137. }
  138.  
  139. public void SaveImg(string WriteImgPath)
  140. {
  141.  
  142. string imgWithMessPath = Path.GetDirectoryName(WriteImgPath) + @"\imgMess.png";
  143.  
  144. this.bmp.Save(imgWithMessPath);
  145.  
  146. this.bmp = null;
  147.  
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment