Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill part of image with color?

Tags:

c#

.net

vb.net

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?

like image 359
Michael Avatar asked Apr 23 '14 21:04

Michael


People also ask

How do I fill a layer with color?

Click the Fill/Adjustment layer icon at the bottom of the Layers panel and select Solid Color. Pick a color from the Color Picker that appears. You can move the round selector to adjust the color, and then click OK. Tip: Drag the vertical slider on the rainbow-colored bar to view a different color range.


1 Answers

A simple ExcludeClip will do:

using (Graphics g = Graphics.FromImage(myImage)) {
  g.ExcludeClip(new Rectangle(100, 100, 100, 200));
  g.Clear(Color.Black);
}
like image 107
LarsTech Avatar answered Sep 18 '22 14:09

LarsTech