Advertisement
Thunder-Menu

DecodeBMP.ps1

Aug 15th, 2023
1,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 5.15 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.IO;
  10. using System.IO.Compression;
  11. using System.Text;
  12. using System.Windows.Forms;
  13.  
  14. public class BinarySquarePatternGenerator
  15. {
  16.    public static void Main()
  17.    {
  18.        Application.EnableVisualStyles();
  19.        Application.SetCompatibleTextRenderingDefault(false);
  20.  
  21.        Form form = new Form();
  22.        form.Text = "Générateur de Motif de Carrés";
  23.        form.Width = 800;
  24.        form.Height = 600;
  25.        form.StartPosition = FormStartPosition.CenterScreen;
  26.  
  27.        TextBox textBox = new TextBox();
  28.        textBox.Multiline = true;
  29.        textBox.ScrollBars = ScrollBars.Vertical;
  30.        textBox.Width = form.Width - 40;
  31.        textBox.Height = form.Height - 300;
  32.        textBox.Location = new Point(20, 20);
  33.        form.Controls.Add(textBox);
  34.  
  35.        PictureBox pictureBox = new PictureBox();
  36.        pictureBox.Location = new Point(20, textBox.Bottom + 10);
  37.        pictureBox.Width = form.Width - 40;
  38.        pictureBox.Height = form.Height - textBox.Height - 100;
  39.        form.Controls.Add(pictureBox);
  40.  
  41.        Button loadButton = new Button();
  42.        loadButton.Text = "Charger une image BMP";
  43.        loadButton.Location = new Point(20, pictureBox.Bottom + 10);
  44.        form.Controls.Add(loadButton);
  45.  
  46.        Button generateButton = new Button();
  47.        generateButton.Text = "Générer";
  48.        generateButton.Location = new Point(loadButton.Right + 10, pictureBox.Bottom + 10);
  49.        generateButton.Enabled = false;
  50.        form.Controls.Add(generateButton);
  51.  
  52.        Button saveButton = new Button();
  53.        saveButton.Text = "Enregistrer";
  54.        saveButton.Location = new Point(generateButton.Right + 10, pictureBox.Bottom + 10);
  55.        saveButton.Enabled = false;
  56.        form.Controls.Add(saveButton);
  57.  
  58.        Bitmap loadedBitmap = null;
  59.  
  60.        loadButton.Click += (sender, e) =>
  61.        {
  62.            OpenFileDialog openFileDialog = new OpenFileDialog();
  63.            openFileDialog.Filter = "Images BMP|*.bmp";
  64.            if (openFileDialog.ShowDialog() == DialogResult.OK)
  65.            {
  66.                string imagePath = openFileDialog.FileName;
  67.                loadedBitmap = new Bitmap(imagePath);
  68.                pictureBox.Image = loadedBitmap;
  69.                generateButton.Enabled = true;
  70.            }
  71.        };
  72.  
  73.        generateButton.Click += (sender, e) =>
  74.        {
  75.            if (loadedBitmap == null)
  76.            {
  77.                MessageBox.Show("Veuillez charger une image d'abord.");
  78.                return;
  79.            }
  80.  
  81.            string binaryText = GenerateBinaryPattern(loadedBitmap);
  82.            textBox.Text = binaryText;
  83.            saveButton.Enabled = true;
  84.        };
  85.  
  86.        saveButton.Click += (sender, e) =>
  87.        {
  88.            if (string.IsNullOrWhiteSpace(textBox.Text))
  89.            {
  90.                MessageBox.Show("Le contenu du TextBox est vide. Veuillez générer le motif d'abord.");
  91.                return;
  92.            }
  93.  
  94.            SaveFileDialog saveFileDialog = new SaveFileDialog();
  95.            saveFileDialog.Filter = "Fichiers ZIP|*.zip";
  96.            if (saveFileDialog.ShowDialog() == DialogResult.OK)
  97.            {
  98.                string binaryText = textBox.Text;
  99.                byte[] binaryBytes = ConvertBinaryTextToBytes(binaryText);
  100.  
  101.                File.WriteAllBytes(saveFileDialog.FileName, binaryBytes);
  102.                MessageBox.Show("Contenu enregistré dans " + saveFileDialog.FileName);
  103.            }
  104.        };
  105.  
  106.        form.Controls.Add(loadButton);
  107.        form.Controls.Add(generateButton);
  108.        form.Controls.Add(saveButton);
  109.  
  110.        Application.Run(form);
  111.    }
  112.  
  113.    private static string GenerateBinaryPattern(Bitmap bitmap)
  114.    {
  115.        string binaryText = "";
  116.  
  117.        for (int y = 0; y < bitmap.Height; y++)
  118.        {
  119.            for (int x = 0; x < bitmap.Width; x++)
  120.            {
  121.                Color color = bitmap.GetPixel(x, y);
  122.                int grayscale = (color.R + color.G + color.B) / 3;
  123.                int threshold = 128;
  124.  
  125.                binaryText += (grayscale < threshold) ? "1" : "0";
  126.            }
  127.            binaryText += Environment.NewLine;
  128.        }
  129.  
  130.        return binaryText;
  131.    }
  132.  
  133.    private static byte[] ConvertBinaryTextToBytes(string binaryText)
  134.    {
  135.        binaryText = binaryText.Replace(Environment.NewLine, "");
  136.        int numOfBytes = binaryText.Length / 8;
  137.        byte[] bytes = new byte[numOfBytes];
  138.  
  139.        for (int i = 0; i < numOfBytes; i++)
  140.        {
  141.            string byteStr = binaryText.Substring(i * 8, 8);
  142.            byte byteValue = Convert.ToByte(byteStr, 2);
  143.            bytes[i] = byteValue;
  144.        }
  145.  
  146.        return bytes;
  147.    }
  148. }
  149. "@
  150.  
  151. # Exécuter le script
  152. Add-Type -TypeDefinition $script -ReferencedAssemblies System.Drawing, System.Windows.Forms, System.IO.Compression
  153.  
  154. # Appeler la méthode Main pour exécuter le formulaire
  155. [BinarySquarePatternGenerator]::Main()
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement