位图上绘制的阴影变得更暗

发布于 2024-12-04 04:52:41 字数 1365 浏览 0 评论 0原文

我正在尝试为位图上动态绘制的矩形创建阴影。问题是每次我绘制一个新的矩形时,阴影都会变暗(请参见屏幕截图)。我怀疑使用相同的位图来绘制新的矩形。我尝试使用 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);
}

在此处输入图像描述

I'm trying to create a shadow to a rectangle drawn dynamically on a bitmap. The problem is the shadow gets darker each time I draw a new rectangle (please see screenshot). I suspect that the same bitmap is used to draw the new rectangles. I tried using Graphics.clear() but it cleans the screen which I don't want.
How can solve this problem?
Here is the code which draws the shadow:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文