Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Charger les assemblies System.Windows.Forms et System.Drawing
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
- # Définir le script en tant que chaîne
- $script = @"
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.IO;
- using System.Text;
- public class BinarySquarePatternGenerator
- {
- public static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Form form = new Form();
- form.Text = "Générateur et Décodeur Binaire";
- form.Width = 800;
- form.Height = 600;
- form.StartPosition = FormStartPosition.CenterScreen;
- TextBox textBox = new TextBox();
- textBox.Multiline = true;
- textBox.ScrollBars = ScrollBars.Vertical;
- textBox.Width = form.Width - 60;
- textBox.Height = form.Height - 330;
- textBox.Location = new Point(20, 20);
- form.Controls.Add(textBox);
- PictureBox pictureBox = new PictureBox();
- pictureBox.Location = new Point(20, textBox.Bottom + 10);
- pictureBox.Width = form.Width - 60;
- pictureBox.Height = form.Height - textBox.Height - 250;
- form.Controls.Add(pictureBox);
- Button loadButton = new Button();
- loadButton.Text = "Charger une image BMP";
- loadButton.Location = new Point(20, pictureBox.Bottom + 10);
- form.Controls.Add(loadButton);
- Button generateButton = new Button();
- generateButton.Text = "Générer";
- generateButton.Location = new Point(loadButton.Right + 10, pictureBox.Bottom + 10);
- generateButton.Enabled = false;
- form.Controls.Add(generateButton);
- Button decodeButton = new Button();
- decodeButton.Text = "Décoder binaire en hex";
- decodeButton.Location = new Point(generateButton.Right + 10, pictureBox.Bottom + 10);
- decodeButton.Enabled = false;
- form.Controls.Add(decodeButton);
- TextBox decodedTextBox = new TextBox();
- decodedTextBox.Multiline = true;
- decodedTextBox.ScrollBars = ScrollBars.Vertical;
- decodedTextBox.Width = form.Width - 60;
- decodedTextBox.Height = form.Height - 330;
- decodedTextBox.Location = new Point(20, decodeButton.Bottom + 10);
- decodedTextBox.Visible = false;
- form.Controls.Add(decodedTextBox);
- Button saveButton = new Button();
- saveButton.Text = "Enregistrer";
- saveButton.Location = new Point(decodeButton.Right + 10, pictureBox.Bottom + 10);
- saveButton.Enabled = false;
- form.Controls.Add(saveButton);
- Bitmap loadedBitmap = null;
- loadButton.Click += (sender, e) =>
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Images BMP|*.bmp";
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- string imagePath = openFileDialog.FileName;
- loadedBitmap = new Bitmap(imagePath);
- pictureBox.Image = loadedBitmap;
- generateButton.Enabled = true;
- decodeButton.Enabled = true;
- saveButton.Enabled = true;
- }
- };
- generateButton.Click += (sender, e) =>
- {
- if (loadedBitmap == null)
- {
- MessageBox.Show("Veuillez charger une image d'abord.");
- return;
- }
- string binaryText = GenerateBinaryPattern(loadedBitmap);
- textBox.Text = binaryText;
- };
- decodeButton.Click += (sender, e) =>
- {
- if (string.IsNullOrWhiteSpace(textBox.Text))
- {
- MessageBox.Show("Le contenu du TextBox est vide. Veuillez générer le motif d'abord.");
- return;
- }
- string hexText = BinaryToHex(textBox.Text);
- decodedTextBox.Text = hexText;
- decodedTextBox.Visible = true;
- };
- saveButton.Click += (sender, e) =>
- {
- if (string.IsNullOrWhiteSpace(decodedTextBox.Text))
- {
- MessageBox.Show("Le contenu du TextBox décodé est vide. Veuillez décoder le motif binaire d'abord.");
- return;
- }
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "Fichiers texte|*.txt";
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- System.IO.File.WriteAllText(saveFileDialog.FileName, decodedTextBox.Text);
- MessageBox.Show("Contenu enregistré dans " + saveFileDialog.FileName);
- }
- };
- form.Controls.Add(loadButton);
- form.Controls.Add(generateButton);
- form.Controls.Add(decodeButton);
- form.Controls.Add(decodedTextBox);
- form.Controls.Add(saveButton);
- Application.Run(form);
- }
- private static string GenerateBinaryPattern(Bitmap bitmap)
- {
- string binaryText = "";
- for (int y = 0; y < bitmap.Height; y++)
- {
- for (int x = 0; x < bitmap.Width; x++)
- {
- Color color = bitmap.GetPixel(x, y);
- int grayscale = (color.R + color.G + color.B) / 3;
- int threshold = 128;
- binaryText += (grayscale < threshold) ? "1" : "0";
- }
- binaryText += Environment.NewLine;
- }
- return binaryText;
- }
- private static string BinaryToHex(string binaryText)
- {
- StringBuilder hexBuilder = new StringBuilder();
- string[] lines = binaryText.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
- foreach (string line in lines)
- {
- StringBuilder lineBuilder = new StringBuilder();
- for (int i = 0; i < line.Length; i += 4)
- {
- string chunk = line.Substring(i, Math.Min(4, line.Length - i));
- int decimalValue = Convert.ToInt32(chunk, 2);
- lineBuilder.Append(decimalValue.ToString("X"));
- }
- hexBuilder.AppendLine(lineBuilder.ToString());
- }
- return hexBuilder.ToString();
- }
- }
- "@
- # Exécuter le script
- Add-Type -TypeDefinition $script -ReferencedAssemblies System.Drawing, System.Windows.Forms
- # Appeler la méthode Main pour exécuter le formulaire
- [BinarySquarePatternGenerator]::Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement