Advertisement
Thunder-Menu

EncodeFileToBmp.ps1

Aug 15th, 2023
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 4.37 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.Drawing.Imaging;
  10. using System.Windows.Forms;
  11. using System.IO.Compression;
  12. using System.IO;
  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; // Ajuster la largeur de la fenêtre
  24.        form.Height = 600; // Ajuster la hauteur de la fenêtre
  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 un fichier ZIP";
  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.        loadButton.Click += (sender, e) =>
  59.        {
  60.            OpenFileDialog openFileDialog = new OpenFileDialog();
  61.            openFileDialog.Filter = "Fichiers ZIP|*.zip";
  62.            if (openFileDialog.ShowDialog() == DialogResult.OK)
  63.            {
  64.                string zipPath = openFileDialog.FileName;
  65.                byte[] binaryData = File.ReadAllBytes(zipPath);
  66.                string hexText = BitConverter.ToString(binaryData).Replace("-", "");
  67.                string binaryText = "";
  68.                foreach (char c in hexText)
  69.                {
  70.                    binaryText += Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0');
  71.                }
  72.                textBox.Text = binaryText;
  73.                generateButton.Enabled = true;
  74.            }
  75.        };
  76.  
  77.        generateButton.Click += (sender, e) =>
  78.        {
  79.            string binaryText = textBox.Text;
  80.            int imageSize = (int)Math.Ceiling(Math.Sqrt(binaryText.Length));
  81.  
  82.            Bitmap bitmap = new Bitmap(imageSize, imageSize);
  83.            for (int i = 0; i < binaryText.Length; i++)
  84.            {
  85.                int x = i % imageSize;
  86.                int y = i / imageSize;
  87.                Color color = (binaryText[i] == '1') ? Color.Black : Color.White;
  88.                bitmap.SetPixel(x, y, color);
  89.            }
  90.  
  91.            pictureBox.Image = bitmap;
  92.            saveButton.Enabled = true;
  93.        };
  94.  
  95.        saveButton.Click += (sender, e) =>
  96.        {
  97.            SaveFileDialog saveFileDialog = new SaveFileDialog();
  98.            saveFileDialog.Filter = "Images BMP|*.bmp";
  99.            if (saveFileDialog.ShowDialog() == DialogResult.OK)
  100.            {
  101.                pictureBox.Image.Save(saveFileDialog.FileName, ImageFormat.Bmp);
  102.                MessageBox.Show("Motif de carrés généré et enregistré sous " + 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. "@
  114.  
  115. # Exécuter le script
  116. Add-Type -TypeDefinition $script -ReferencedAssemblies System.Drawing, System.Windows.Forms
  117.  
  118. # Appeler la méthode Main pour exécuter le formulaire
  119. [BinarySquarePatternGenerator]::Main()
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement