Masking/Blackening particular area of images in a folder (c#)

82 views Asked by At

Recently i created a small application, where i select an image and mask/blackening its area, by providing coordinates.based on coordinates, its covering that part of image (can be seen here in red color). enter image description here

Code 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 extend this, where i dont want to select image, rather wanted to select folders.

A A1 A2

B B1 B2

There are many image files available in "A" folders and "B" folders. once i provide coordinates, at one go i want to mask all images, like 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]);
        }

    }
0

There are 0 answers