【问题标题】:Add dropshadow to a TBitmap将阴影添加到 TBitmap
【发布时间】:2012-06-06 17:03:20
【问题描述】:

我有一个截取屏幕截图 (TBitmap) 的例程,我需要在最终的 TBitmap/图像中添加阴影,我有这段代码(它曾经可以工作,但是...)有些不对劲,drop -shadow 根本不被绘制:

// --------------------------------------------------------------------- //
procedure TakeScreenshot();
var
   lCapRect : TRect;
   DestBitmap : TBitmap;
begin
     // Take the screenshot & assign it to DestBitmap
     // ...

     // Add the drop shadow to DestBitmap
     DestBitmap.Width  := DestBitmap.Width + 6;
     DestBitmap.Height := DestBitmap.Height + 6;

     PaintShadow(DestBitmap.Canvas, lCapRect);
end;
// --------------------------------------------------------------------- //
procedure PaintShadow(ACanvas : TCanvas; ARect : TRect);
var
   AColor         : TColor;
   i, iMax        : Integer;
   h1, h2, v1, v2 : Integer;
begin
     AColor := ACanvas.Brush.Color;
     iMax   := 6;
     h1     := ARect.Left;
     h2     := ARect.Right;
     v1     := ARect.Top;
     v2     := ARect.Bottom;

     with ACanvas do
     begin
      for i := iMax downto 0 do
      begin
           ACanvas.Pen.Mode := pmMask;
           Pen.Color        := DarkenColorBy(AColor, ((iMax - i) * 4 + 10));

           MoveTo(h1 + 4{i}, v2 + i);
           LineTo(h2 + i + 1, v2 + i);
      end;    // for

      for i := iMax downto 0 do
      begin
           ACanvas.Pen.Mode := pmMask;
           Pen.Color        := DarkenColorBy(AColor, ((iMax - i) * 4 + 10));

           MoveTo(h2 + i, v1 + 4{i});
           LineTo(h2 + i, v2 + i);
      end;    // for
     end;    // with
end;
// --------------------------------------------------------------------- //
function Max(const A, B: Integer): Integer;
begin
     if (A > B) then
    Result  := A
     else
     Result := B;
end;
// --------------------------------------------------------------------- //
function DarkenColorBy(BaseColor : TColor; Amount : Integer) : TColor;
begin
     Result := RGB(Max(GetRValue(ColorToRGB(BaseColor)) - Amount, 0),
           Max(GetGValue(ColorToRGB(BaseColor)) - Amount, 0),
           Max(GetBValue(ColorToRGB(BaseColor)) - Amount, 0));
end;

我的问题是:我该如何解决这个问题(或者任何人都知道将阴影添加到 TBitmap 的简单方法)?

最终的图像是要保存为 bmp/jpg 的,不显示在 TImage 中,所以我真的需要给图像本身添加阴影。

PS。我使用的是 Delphi 7 Pro,我的应用仅限于 Windows XP 或更高版本

编辑

lCapRect 取决于设置(我是捕获活动监视器、窗口还是所有桌面监视器),但假设它是这样计算的:

lCapRect.Right  := Screen.DesktopLeft + Screen.DesktopWidth;
lCapRect.Bottom := Screen.DesktopTop + Screen.DesktopHeight;
lCapRect.Left   := Screen.DesktopLeft;
lCapRect.Top    := Screen.DesktopTop;

位图确实包含屏幕截图(+ 6 个像素添加到底部和右侧为阴影腾出空间),只是没有发生阴影绘制

【问题讨论】:

  • 你为 'lCapRect' 传递了什么?
  • 我会推荐 GDI+ (bilsen.com/gdiplus/index.shtml),它能够创建具有 alpha 值的画笔,但不幸的是它只有 D2009 及更高版本
  • @AlanClark:谢谢,我担心 Delphi 7 需要这个 :(

标签: delphi image-processing delphi-7 vcl dropshadow


【解决方案1】:

你还没有展示你是如何计算lCapRect的。为了不绘制关于您的PaintShadow 程序的位图,它必须小于位图,例如:

lCapRect := DestBitmap.Canvas.ClipRect;

// Add the drop shadow to DestBitmap
DestBitmap.Width  := DestBitmap.Width + 6;
DestBitmap.Height := DestBitmap.Height + 6;

PaintShadow(DestBitmap.Canvas, lCapRect);

【讨论】:

  • 我编辑了关于 lCapRect 的帖子。我在底部/右侧都添加了 6 个像素,为阴影效果腾出空间
  • @Gdhami - 不要单独计算 lCapRect,你已经有了一个确定大小的位图。你试过答案中的代码吗?
  • 你说得对,我没有注意 Canvas.ClipRect 赋值,阴影有点难看,但那是另一回事,至少现在绘图发生了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 2013-02-15
  • 2016-08-09
  • 1970-01-01
相关资源
最近更新 更多