阴影在位图上绘制得更黑

时间:2011-09-11 20:15:37

标签: c# bitmap gdi

我正在尝试为位图上动态绘制的矩形创建阴影。问题是每次绘制新矩形时阴影都会变暗(请参见屏幕截图)。我怀疑使用相同的位图来绘制新的矩形。我尝试使用Graphics.clear(),但它清理了我不想要的屏幕。
如何解决这个问题?
这是绘制阴影的代码:

public void drawAll(Rectangle baseRect,Graphics g)
{
  int shadWidth = 10;   
  Bitmap bm = new Bitmap(shadWidth, baseRect.Height+shadWidth);//baseRect is created dynamically
  for (int y = 0; y < baseRect.Height + shadWidth; y++)
  {
     int factor = 255 / shadWidth;//255 is the alpha color divided over the shadow width
     int alpha = 255;
     for (int x = 0; x < shadWidth; x++)
      {
         alpha -= factor;
         if (alpha < 0) alpha = 0;
         Color transColr = Color.FromArgb(alpha, 0, 0, 0);
         bm.SetPixel(x, y, transColr);
       }
   }
  GraphicsPath path = new GraphicsPath();
  PointF[] pts = new[] {new PointF(baseRect.Right, baseRect.Top),
               new PointF(baseRect.Right+shadWidth, baseRect.Top+shadWidth),
               new PointF(baseRect.Right+shadWidth, baseRect.Bottom+shadWidth),
               new PointF(baseRect.Right, baseRect.Bottom),
               new PointF(baseRect.Right, baseRect.Top)};
  path.AddLines(pts);
  SmoothingMode old = g.SmoothingMode;
  g.SmoothingMode = SmoothingMode.AntiAlias;
  g.DrawImageUnscaled(bm, baseRect.Right, baseRect.Y);
}

enter image description here

0 个答案:

没有答案