How to fill part of the image with color?

I have a bitmap:

enter image description here

I draw a rectangle in this 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 except for the rectangle. Like this: enter image description here

Any idea how to implement it?

+6
source share
1 answer

A simple ExcludeClip will do:

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

All Articles