Advertisement
Thunder-Menu

square_1200dpi.ps1

Aug 14th, 2023
1,152
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.            float dpi = 1200f;
  57.            float tailleCarreMM = 0.021f;
  58.            float espaceEntreCarresMM = 0.021f;
  59.  
  60.            int tailleCarrePixels = (int)Math.Round(dpi * tailleCarreMM / 25.4f);
  61.            int espaceEntreCarresPixels = (int)Math.Round(dpi * espaceEntreCarresMM / 25.4f);
  62.  
  63.            Bitmap bitmap = new Bitmap(largeurPagePixels, hauteurPagePixels);
  64.            Graphics graphics = Graphics.FromImage(bitmap);
  65.  
  66.            SolidBrush whiteBrush = new SolidBrush(Color.White);
  67.            SolidBrush blackBrush = new SolidBrush(Color.Black);
  68.  
  69.            graphics.FillRectangle(whiteBrush, 0, 0, largeurPagePixels, hauteurPagePixels);
  70.  
  71.            int carreCompteur = 0;
  72.            for (int x = 0; x < largeurPagePixels; x += (tailleCarrePixels + espaceEntreCarresPixels))
  73.            {
  74.                for (int y = 0; y < hauteurPagePixels; y += (tailleCarrePixels + espaceEntreCarresPixels))
  75.                {
  76.                    graphics.FillRectangle(blackBrush, x, y, tailleCarrePixels, tailleCarrePixels);
  77.  
  78.                    carreCompteur++;
  79.                    if (carreCompteur >= nombreDeCarres)
  80.                    {
  81.                        break;
  82.                    }
  83.                }
  84.                if (carreCompteur >= nombreDeCarres)
  85.                {
  86.                    break;
  87.                }
  88.            }
  89.  
  90.            pictureBox.Image = bitmap;
  91.            saveButton.Enabled = true;
  92.        };
  93.  
  94.        saveButton.Click += (sender, e) =>
  95.        {
  96.            SaveFileDialog saveFileDialog = new SaveFileDialog();
  97.            saveFileDialog.Filter = "Images BMP|*.bmp";
  98.            if (saveFileDialog.ShowDialog() == DialogResult.OK)
  99.            {
  100.                pictureBox.Image.Save(saveFileDialog.FileName, ImageFormat.Bmp);
  101.                MessageBox.Show("Motif de carrés généré et enregistré sous " + saveFileDialog.FileName);
  102.            }
  103.        };
  104.  
  105.        form.Controls.Add(saveButton);
  106.  
  107.        Application.Run(form);
  108.    }
  109. }
  110. "@
  111.  
  112. # Exécuter le script
  113. Add-Type -TypeDefinition $script -ReferencedAssemblies System.Drawing, System.Windows.Forms
  114.  
  115. # Appeler la méthode Main pour exécuter le formulaire
  116. [SquarePatternGenerator]::Main()
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement