Advertisement
Thunder-Menu

BMPTOFILE.ps1

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