如何用颜色填充部分图像?

时间:2014-04-23 21:13:17

标签: c# .net vb.net

我有位图图片:

enter image description here

我在该图像上绘制矩形:

    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

我想用黑色填充整个图像,矩形除外。 像这样: enter image description here

知道如何实现它吗?

1 个答案:

答案 0 :(得分:9)

一个简单的ExcludeClip可以:

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