Thursday, April 9, 2009

Draw a translucent effect image In Delphi

We may use windows GDI function AlphaBlend to draw translucent graphic. The following example extract a portion of desktop image and draw to TImage.canvas with translucent effect. The resulting graphic will looks like there is a green color transparent sheet cover on the image.

var hDesktop: HWND;
DC: HDC;
T: BLENDFUNCTION;
begin
hDesktop := GetDesktopWindow;
DC := GetDC(hDesktop);
try
T.BlendOp := AC_SRC_OVER;
T.BlendFlags := 0;
T.SourceConstantAlpha := $80;
T.AlphaFormat := 0;

Image1.Canvas.Brush.Color := clGreen;
Image1.Canvas.FillRect(Rect(0, 0, Image1.Width, Image1.Height));
Windows.AlphaBlend(Image1.Canvas.Handle, 0, 0, Image1.Width, Image1.Height, DC, 0, 0, Image1.Width, Image1.Height, T);
finally
ReleaseDC(hDesktop, DC);
end;
end;

No comments: