Advertisement
Thunder-Menu

Square2-1.ps1

Aug 14th, 2023
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  12. public class SquarePatternGenerator
  13. {
  14.    public static void Main()
  15.    {
  16.        Application.EnableVisualStyles();
  17.        Application.SetCompatibleTextRenderingDefault(false);
  18.  
  19.        Form form = new Form();
  20.        form.Text = "Générateur de Motif de Carrés";
  21.        form.Width = 600; // Ajuster la largeur de la fenêtre
  22.        form.Height = 800; // Ajuster la hauteur de la fenêtre
  23.        form.StartPosition = FormStartPosition.CenterScreen;
  24.  
  25.        PictureBox pictureBox = new PictureBox();
  26.        pictureBox.Location = new Point(20, 100);
  27.        pictureBox.Width = form.Width - 40;
  28.        pictureBox.Height = form.Height - 300;
  29.        form.Controls.Add(pictureBox);
  30.  
  31.        Label label = new Label();
  32.        label.Text = "Nombre de carrés:";
  33.        label.Location = new Point(20, 20);
  34.        form.Controls.Add(label);
  35.  
  36.        TextBox textBox = new TextBox();
  37.        textBox.Location = new Point(150, 20);
  38.        form.Controls.Add(textBox);
  39.  
  40.        Button generateButton = new Button();
  41.        generateButton.Text = "Générer";
  42.        generateButton.Location = new Point(20, pictureBox.Bottom + 10);
  43.        form.Controls.Add(generateButton);
  44.  
  45.        Button saveButton = new Button();
  46.        saveButton.Text = "Enregistrer";
  47.        saveButton.Location = new Point(100, pictureBox.Bottom + 10);
  48.        saveButton.Enabled = false;
  49.        form.Controls.Add(saveButton);
  50.  
  51.        generateButton.Click += (sender, e) =>
  52.        {
  53.            int nombreDeCarres = int.Parse(textBox.Text);
  54.            int largeurPagePixels = pictureBox.Width;
  55.            int hauteurPagePixels = pictureBox.Height;
  56.            int tailleCarrePixels = 2;
  57.            int espaceEntreCarresPixels = 1;
  58.  
  59.            Bitmap bitmap = new Bitmap(largeurPagePixels, hauteurPagePixels);
  60.            Graphics graphics = Graphics.FromImage(bitmap);
  61.  
  62.            SolidBrush whiteBrush = new SolidBrush(Color.White);
  63.            SolidBrush blackBrush = new SolidBrush(Color.Black);
  64.  
  65.            graphics.FillRectangle(whiteBrush, 0, 0, largeurPagePixels, hauteurPagePixels);
  66.  
  67.            int carreCompteur = 0;
  68.            for (int x = 0; x < largeurPagePixels; x += (tailleCarrePixels + espaceEntreCarresPixels))
  69.            {
  70.                for (int y = 0; y < hauteurPagePixels; y += (tailleCarrePixels + espaceEntreCarresPixels))
  71.                {
  72.                    graphics.FillRectangle(blackBrush, x, y, tailleCarrePixels, tailleCarrePixels);
  73.  
  74.                    carreCompteur++;
  75.                    if (carreCompteur >= nombreDeCarres)
  76.                    {
  77.                        break;
  78.                    }
  79.                }
  80.                if (carreCompteur >= nombreDeCarres)
  81.                {
  82.                    break;
  83.                }
  84.            }
  85.  
  86.            pictureBox.Image = bitmap;
  87.            saveButton.Enabled = true;
  88.        };
  89.  
  90.        saveButton.Click += (sender, e) =>
  91.        {
  92.            SaveFileDialog saveFileDialog = new SaveFileDialog();
  93.            saveFileDialog.Filter = "Images BMP|*.bmp";
  94.            if (saveFileDialog.ShowDialog() == DialogResult.OK)
  95.            {
  96.                pictureBox.Image.Save(saveFileDialog.FileName, ImageFormat.Bmp);
  97.                MessageBox.Show("Motif de carrés généré et enregistré sous " + saveFileDialog.FileName);
  98.            }
  99.        };
  100.  
  101.        form.Controls.Add(saveButton);
  102.  
  103.        Application.Run(form);
  104.    }
  105. }
  106. "@
  107.  
  108. # Exécuter le script
  109. Add-Type -TypeDefinition $script -ReferencedAssemblies System.Drawing, System.Windows.Forms
  110.  
  111. # Appeler la méthode Main pour exécuter le formulaire
  112. [SquarePatternGenerator]::Main()
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement