2012-06-06 249 views
2

我有一个需要截图(TBitmap)的例程,我需要将阴影添加到最终的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专业版,我的应用程序仅限于Windows XP或更高

编辑

lCapRect取决于设置(我是否拍摄活动监视器,窗口或全部桌面显示器),但是我们要说它是这样计算:

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

确实包含截图位图(+ 6个像素加到底部&右侧,以腾出空间阴影效果),它只是在阴影绘图不发生

+0

什么是你传递的“lCapRect”? –

+0

我会推荐GDI +(http://www.bilsen.com/gdiplus/index.shtml),它具有创建带有alpha值的画笔的功能,但不幸的是它只有D2009,而且只有 –

+0

@AlanClark:谢谢,我是恐怕我需要这个德尔福7 :( – TheDude

回答

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); 
+0

我编辑我的帖子关于lCapRect。我添加了6像素的底部/右侧为腾出空间的阴影效果 – TheDude

+1

@Gdhami - 不要独立计算lCapRect,你已经有一个位图你有没有试过答案中的代码? –

+0

你是对的,我没有注意到Canvas.ClipRect分配,投影有点难看,但这是一个不同的故事,至少现在绘图发生,谢谢! – TheDude