Advertisement
Thunder-Menu

BMPFileBinaryDecoder.ps1

Aug 14th, 2023
1,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 6.50 KB | Source Code | 0 0
  1. # Charger les assemblies System.Windows.Forms et System.Drawing
  2. Add-Type -AssemblyName System.Windows.Forms
  3. Add-Type -AssemblyName System.Drawing
  4.  
  5. # Définir le script en tant que chaîne
  6. $script = @"
  7. using System;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Text;
  12.  
  13. public class BinarySquarePatternGenerator
  14. {
  15.    public static void Main()
  16.    {
  17.        Application.EnableVisualStyles();
  18.        Application.SetCompatibleTextRenderingDefault(false);
  19.  
  20.        Form form = new Form();
  21.        form.Text = "Générateur et Décodeur Binaire";
  22.        form.Width = 800;
  23.        form.Height = 600;
  24.        form.StartPosition = FormStartPosition.CenterScreen;
  25.  
  26.        TextBox textBox = new TextBox();
  27.        textBox.Multiline = true;
  28.        textBox.ScrollBars = ScrollBars.Vertical;
  29.        textBox.Width = form.Width - 60;
  30.        textBox.Height = form.Height - 330;
  31.        textBox.Location = new Point(20, 20);
  32.        form.Controls.Add(textBox);
  33.  
  34.        PictureBox pictureBox = new PictureBox();
  35.        pictureBox.Location = new Point(20, textBox.Bottom + 10);
  36.        pictureBox.Width = form.Width - 60;
  37.        pictureBox.Height = form.Height - textBox.Height - 250;
  38.        form.Controls.Add(pictureBox);
  39.  
  40.        Button loadButton = new Button();
  41.        loadButton.Text = "Charger une image BMP";
  42.        loadButton.Location = new Point(20, pictureBox.Bottom + 10);
  43.        form.Controls.Add(loadButton);
  44.  
  45.        Button generateButton = new Button();
  46.        generateButton.Text = "Générer";
  47.        generateButton.Location = new Point(loadButton.Right + 10, pictureBox.Bottom + 10);
  48.        generateButton.Enabled = false;
  49.        form.Controls.Add(generateButton);
  50.  
  51.        Button decodeButton = new Button();
  52.        decodeButton.Text = "Décoder binaire en hex";
  53.        decodeButton.Location = new Point(generateButton.Right + 10, pictureBox.Bottom + 10);
  54.        decodeButton.Enabled = false;
  55.        form.Controls.Add(decodeButton);
  56.  
  57.        TextBox decodedTextBox = new TextBox();
  58.        decodedTextBox.Multiline = true;
  59.        decodedTextBox.ScrollBars = ScrollBars.Vertical;
  60.        decodedTextBox.Width = form.Width - 60;
  61.        decodedTextBox.Height = form.Height - 330;
  62.        decodedTextBox.Location = new Point(20, decodeButton.Bottom + 10);
  63.        decodedTextBox.Visible = false;
  64.        form.Controls.Add(decodedTextBox);
  65.  
  66.        Button saveButton = new Button();
  67.        saveButton.Text = "Enregistrer";
  68.        saveButton.Location = new Point(decodeButton.Right + 10, pictureBox.Bottom + 10);
  69.        saveButton.Enabled = false;
  70.        form.Controls.Add(saveButton);
  71.  
  72.        Bitmap loadedBitmap = null;
  73.  
  74.        loadButton.Click += (sender, e) =>
  75.        {
  76.            OpenFileDialog openFileDialog = new OpenFileDialog();
  77.            openFileDialog.Filter = "Images BMP|*.bmp";
  78.            if (openFileDialog.ShowDialog() == DialogResult.OK)
  79.            {
  80.                string imagePath = openFileDialog.FileName;
  81.                loadedBitmap = new Bitmap(imagePath);
  82.                pictureBox.Image = loadedBitmap;
  83.                generateButton.Enabled = true;
  84.                decodeButton.Enabled = true;
  85.                saveButton.Enabled = true;
  86.            }
  87.        };
  88.  
  89.        generateButton.Click += (sender, e) =>
  90.        {
  91.            if (loadedBitmap == null)
  92.            {
  93.                MessageBox.Show("Veuillez charger une image d'abord.");
  94.                return;
  95.            }
  96.  
  97.            string binaryText = GenerateBinaryPattern(loadedBitmap);
  98.            textBox.Text = binaryText;
  99.        };
  100.  
  101.        decodeButton.Click += (sender, e) =>
  102.        {
  103.            if (string.IsNullOrWhiteSpace(textBox.Text))
  104.            {
  105.                MessageBox.Show("Le contenu du TextBox est vide. Veuillez générer le motif d'abord.");
  106.                return;
  107.            }
  108.  
  109.            string hexText = BinaryToHex(textBox.Text);
  110.            decodedTextBox.Text = hexText;
  111.            decodedTextBox.Visible = true;
  112.        };
  113.  
  114.        saveButton.Click += (sender, e) =>
  115.        {
  116.            if (string.IsNullOrWhiteSpace(decodedTextBox.Text))
  117.            {
  118.                MessageBox.Show("Le contenu du TextBox décodé est vide. Veuillez décoder le motif binaire d'abord.");
  119.                return;
  120.            }
  121.  
  122.            SaveFileDialog saveFileDialog = new SaveFileDialog();
  123.            saveFileDialog.Filter = "Fichiers texte|*.txt";
  124.            if (saveFileDialog.ShowDialog() == DialogResult.OK)
  125.            {
  126.                System.IO.File.WriteAllText(saveFileDialog.FileName, decodedTextBox.Text);
  127.                MessageBox.Show("Contenu enregistré dans " + saveFileDialog.FileName);
  128.            }
  129.        };
  130.  
  131.        form.Controls.Add(loadButton);
  132.        form.Controls.Add(generateButton);
  133.        form.Controls.Add(decodeButton);
  134.        form.Controls.Add(decodedTextBox);
  135.        form.Controls.Add(saveButton);
  136.  
  137.        Application.Run(form);
  138.    }
  139.  
  140.    private static string GenerateBinaryPattern(Bitmap bitmap)
  141.    {
  142.        string binaryText = "";
  143.  
  144.        for (int y = 0; y < bitmap.Height; y++)
  145.        {
  146.            for (int x = 0; x < bitmap.Width; x++)
  147.            {
  148.                Color color = bitmap.GetPixel(x, y);
  149.                int grayscale = (color.R + color.G + color.B) / 3;
  150.                int threshold = 128;
  151.  
  152.                binaryText += (grayscale < threshold) ? "1" : "0";
  153.            }
  154.            binaryText += Environment.NewLine;
  155.        }
  156.  
  157.        return binaryText;
  158.    }
  159.  
  160.    private static string BinaryToHex(string binaryText)
  161.    {
  162.        StringBuilder hexBuilder = new StringBuilder();
  163.  
  164.        string[] lines = binaryText.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
  165.  
  166.        foreach (string line in lines)
  167.        {
  168.            StringBuilder lineBuilder = new StringBuilder();
  169.            for (int i = 0; i < line.Length; i += 4)
  170.            {
  171.                string chunk = line.Substring(i, Math.Min(4, line.Length - i));
  172.                int decimalValue = Convert.ToInt32(chunk, 2);
  173.                lineBuilder.Append(decimalValue.ToString("X"));
  174.            }
  175.            hexBuilder.AppendLine(lineBuilder.ToString());
  176.        }
  177.  
  178.        return hexBuilder.ToString();
  179.    }
  180. }
  181. "@
  182.  
  183. # Exécuter le script
  184. Add-Type -TypeDefinition $script -ReferencedAssemblies System.Drawing, System.Windows.Forms
  185.  
  186. # Appeler la méthode Main pour exécuter le formulaire
  187. [BinarySquarePatternGenerator]::Main()
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement