Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Threading;
- using System.Text.RegularExpressions;
- namespace ImageToBinary
- {
- public partial class Form1 : Form
- {
- string binary = "";
- string text = "";
- bool isText = true;
- public Form1()
- {
- InitializeComponent();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- // Show the dialog and get result.
- DialogResult result = openFileDialog1.ShowDialog();
- if (result == DialogResult.OK) // Test result.
- {
- textBox1.Text = (string)openFileDialog1.FileName;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Bitmap b = GetBitmap(textBox1.Text);
- string test = "";
- Log("This will take a few minutes");
- Log("Loaded image");
- Log("Decoding image...");
- for (int i = 0; i < b.Width; i += 16)
- {
- for (int j = 0; j < b.Height; j+= 5)
- {
- if (i + 16 >= b.Width)
- {
- //Do nothing
- }
- else
- {
- for (int n = i + 16; n > i; n -= 4)
- {
- if (b.GetPixel(n, j).B == 255)
- {
- test = test + "1";
- }
- else
- {
- if (b.GetPixel(n - 1, j).B == 255)
- {
- test = test + "1";
- }
- else if (b.GetPixel(n - 1, j).B != 255)
- {
- test = test + "0";
- }
- }
- }
- }
- }
- }
- Log("Decoded to binary!");
- Log("First 4 binary: " + test.Substring(0, 4));
- Log("Length: " + test.Length);
- isText = true;
- binary = test;
- List<string> srrr = Split(test, 8).ToList();
- List<int> FinalInts = new List<int>();
- foreach (string s in srrr)
- {
- FinalInts.Add(Convert.ToInt32(s, 2));
- }
- string finalString = "";
- foreach (int i in FinalInts)
- {
- finalString += (char)i;
- }
- Regex rgx = new Regex("[^a-zA-Z0-9 #]");
- richTextBox2.Text = rgx.Replace(finalString, "");
- text = rgx.Replace(finalString, "");
- Log("Done!");
- }
- static IEnumerable<string> Split(string str, int chunkSize)
- {
- return Enumerable.Range(0, str.Length / chunkSize)
- .Select(i => str.Substring(i * chunkSize, chunkSize));
- }
- public static Bitmap GetBitmap(string filename)
- {
- Bitmap retBitmap = null;
- if (File.Exists(filename))
- {
- try
- {
- retBitmap = new Bitmap(filename, true);
- }
- catch { }
- }
- return retBitmap;
- }
- public void Log(string text)
- {
- richTextBox1.Text += text + "\n";
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (isText)
- {
- richTextBox2.Text = binary;
- isText = false;
- }
- else
- {
- richTextBox2.Text = text;
- isText = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement