2

我有一个截取屏幕截图(TBitmap)的例程,我需要在最终的 TBitmap/图像中添加阴影,我有这段代码(以前可以工作,但是......)有些不对劲,只是阴影未绘制:

// --------------------------------------------------------------------- //
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 个像素以为阴影腾出空间),只是没有发生阴影绘制

4

1 回答 1

3

你还没有显示你是如何计算的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);
于 2012-06-06T17:46:40.110 回答