Advertisement
Guest User

Untitled

a guest
Sep 11th, 2012
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Text.RegularExpressions;
  12.  
  13. namespace ImageToBinary
  14. {
  15. public partial class Form1 : Form
  16. {
  17. string binary = "";
  18. string text = "";
  19. bool isText = true;
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. private void button2_Click(object sender, EventArgs e)
  26. {
  27. // Show the dialog and get result.
  28. DialogResult result = openFileDialog1.ShowDialog();
  29. if (result == DialogResult.OK) // Test result.
  30. {
  31. textBox1.Text = (string)openFileDialog1.FileName;
  32. }
  33. }
  34.  
  35. private void button1_Click(object sender, EventArgs e)
  36. {
  37. Bitmap b = GetBitmap(textBox1.Text);
  38. string test = "";
  39. Log("This will take a few minutes");
  40. Log("Loaded image");
  41. Log("Decoding image...");
  42. for (int i = 0; i < b.Width; i += 16)
  43. {
  44. for (int j = 0; j < b.Height; j+= 5)
  45. {
  46. if (i + 16 >= b.Width)
  47. {
  48. //Do nothing
  49. }
  50. else
  51. {
  52. for (int n = i + 16; n > i; n -= 4)
  53. {
  54. if (b.GetPixel(n, j).B == 255)
  55. {
  56. test = test + "1";
  57. }
  58. else
  59. {
  60. if (b.GetPixel(n - 1, j).B == 255)
  61. {
  62. test = test + "1";
  63. }
  64. else if (b.GetPixel(n - 1, j).B != 255)
  65. {
  66. test = test + "0";
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. Log("Decoded to binary!");
  74. Log("First 4 binary: " + test.Substring(0, 4));
  75. Log("Length: " + test.Length);
  76. isText = true;
  77. binary = test;
  78. List<string> srrr = Split(test, 8).ToList();
  79. List<int> FinalInts = new List<int>();
  80. foreach (string s in srrr)
  81. {
  82. FinalInts.Add(Convert.ToInt32(s, 2));
  83. }
  84. string finalString = "";
  85. foreach (int i in FinalInts)
  86. {
  87. finalString += (char)i;
  88. }
  89. Regex rgx = new Regex("[^a-zA-Z0-9 #]");
  90. richTextBox2.Text = rgx.Replace(finalString, "");
  91. text = rgx.Replace(finalString, "");
  92. Log("Done!");
  93. }
  94.  
  95. static IEnumerable<string> Split(string str, int chunkSize)
  96. {
  97. return Enumerable.Range(0, str.Length / chunkSize)
  98. .Select(i => str.Substring(i * chunkSize, chunkSize));
  99. }
  100.  
  101. public static Bitmap GetBitmap(string filename)
  102. {
  103. Bitmap retBitmap = null;
  104. if (File.Exists(filename))
  105. {
  106. try
  107. {
  108. retBitmap = new Bitmap(filename, true);
  109. }
  110. catch { }
  111. }
  112. return retBitmap;
  113. }
  114.  
  115. public void Log(string text)
  116. {
  117. richTextBox1.Text += text + "\n";
  118. }
  119.  
  120. private void button3_Click(object sender, EventArgs e)
  121. {
  122. if (isText)
  123. {
  124. richTextBox2.Text = binary;
  125. isText = false;
  126. }
  127. else
  128. {
  129. richTextBox2.Text = text;
  130. isText = true;
  131. }
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement