Masking / Blackening a specific area of ​​images in a folder (C #)

I recently created a small application where I select an image and mask / black out its area, providing coordinates based on coordinates, covering that part of the image (can be seen here in red). enter image description here

The code is here:

    private void Mask_button_Click(object sender, EventArgs e)
    {
        if (textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "" || textBox5.Text.Trim() == "" || textBox6.Text.Trim() == "" || textBox7.Text.Trim() == "" || textBox8.Text.Trim() == "" || textBox9.Text.Trim() == "" || textBox10.Text.Trim() == "" || textBox11.Text.Trim() == "" || textBox12.Text.Trim() == "" || textBox13.Text.Trim() == "" || textBox3.Text.Trim() == "")
        {
            MessageBox.Show("All value in above text boxes are compulsory");
            return;
        }
        if(pictureBox1.Image==null)
        {
            MessageBox.Show("No Image Selected");
            return;
        }
        using (var g = Graphics.FromImage(pictureBox1.Image))
        {
            g.FillRectangle(Brushes.Tomato, Int32.Parse(textBox3.Text), Int32.Parse(textBox4.Text), Int32.Parse(textBox5.Text), Int32.Parse(textBox6.Text));
            g.FillRectangle(Brushes.Tomato, Int32.Parse(textBox7.Text), Int32.Parse(textBox8.Text), Int32.Parse(textBox9.Text), Int32.Parse(textBox10.Text));
            g.FillRectangle(Brushes.Tomato, Int32.Parse(textBox11.Text), Int32.Parse(textBox12.Text), Int32.Parse(textBox13.Text), Int32.Parse(textBox14.Text));
            pictureBox1.Invalidate();
        }
    }

      

Now I wanted to expand this where I do not want to select an image, but I would like to select folders.

A A1 A2

B B1 B2

There are many image files in folders "A" and "B". when i provide coordinates i want to hide all images, for example i did on top.

Code changes:

private void slide_button_Click (object sender, EventArgs e) {var directory = new DirectoryInfo (@ "D: \ 2Images"); var allowedExtensions = new string [] {".jpg", ".bmp", ".png"};

        var imageFiles = from file in directory.EnumerateFiles("*", SearchOption.AllDirectories)
                         where allowedExtensions.Contains(file.Extension.ToLower())
                         select file;
        List<string> new1 = new List<string>();

        foreach (var file in imageFiles)
            new1.Add(file.ToString());

        for (int i = 0; i < new1.Count; i++)
        {
            Image bitmap = Image.FromFile(@"D:\2Images\" + new1[i]);
            byte[] bytes = System.IO.File.ReadAllBytes(@"D:\2Images\" + new1[i]);
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
            pictureBox1.Image = Image.FromStream(ms);
            bm = new Bitmap(pictureBox1.Image);
            var g = Graphics.FromImage(bm);
            g.FillRectangle(Brushes.Tomato, Int32.Parse(textBox3.Text), Int32.Parse(textBox4.Text), Int32.Parse(textBox5.Text), Int32.Parse(textBox6.Text));
            g.FillRectangle(Brushes.Tomato, Int32.Parse(textBox7.Text), Int32.Parse(textBox8.Text), Int32.Parse(textBox9.Text), Int32.Parse(textBox10.Text));
            g.FillRectangle(Brushes.Tomato, Int32.Parse(textBox11.Text), Int32.Parse(textBox12.Text), Int32.Parse(textBox13.Text), Int32.Parse(textBox14.Text));
            pictureBox1.Invalidate();
            bm.Save(@"D:\3Images\" + new1[i]);
        }

    }

      

+3


source to share





All Articles