Michael
Michael

Reputation: 13636

How to fill part of image with color?

I have Bitmap image:

enter image description here

I draw rectangle on that image:

    Bitmap myImage = new Bitmap("path");
    using (Graphics gr = Graphics.FromImage(myImage)) 
    {
       Pen pen = new Pen(Color.Black, 2);
       gr.DrawRectangle(pen, 100,100, 100, 200);
    }

enter image description here

I want to fill the entire image with black color, except the rectangle. Like this: enter image description here

Any idea how to implement it?

Upvotes: 7

Views: 1338

Answers (1)

LarsTech
LarsTech

Reputation: 81675

A simple ExcludeClip will do:

using (Graphics g = Graphics.FromImage(myImage)) {
  g.ExcludeClip(new Rectangle(100, 100, 100, 200));
  g.Clear(Color.Black);
}

Upvotes: 11

Related Questions