简体   繁体   中英

How to fill part of image with color?

I have Bitmap image:

在此输入图像描述

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);
    }

在此输入图像描述

I want to fill the entire image with black color, except the rectangle. Like this: 在此输入图像描述

Any idea how to implement it?

A simple ExcludeClip will do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM