How can I write a semi transparent text on an Image (Jpg,Bmp), or a transparent text (color as same background Image) but with a shadow, something I want to do to watermark the images.
I want to accomplish that using Delphi win32.
One option is to use the AlphaBlend function in Windows.pas unit. Something like this will produce semi-transparent text (with a drop shadow - building on Jim McKeeth's response) overlayed on an image:
uses Windows, Graphics;
.
.
.
var
BackgroundImage: Graphics.TBitmap; { need to call out specifically for Graphics.TBitmap
because the Windows unit also has a TBitmap
declaration }
TextImage: Graphics.TBitmap;
BlendFunc: BLENDFUNCTION;
begin
BlendFunc.BlendOp := AC_SRC_OVER;
BlendFunc.BlendFlags := 0;
BlendFunc.SourceConstantAlpha := $C0; { a hex value from $00-$FF (0-255).
Represents the percent of opaqueness:
$00 is completely transparent,
$FF is completely opaque.
$C0 is 75% opaque }
BlendFunc.AlphaFormat := AC_SRC_ALPHA;
{ BackgroundImage is for holding the image you want to overlay text onto }
BackgroundImage := Graphics.TBitmap.Create;
try
BackgroundImage.LoadFromFile('yourimagehere.bmp');
{ Create another TBitmap to hold the text you want to overlay }
TextImage := Graphics.TBitmap.Create;
try
{ Set this bitmap to have the same dimensions as the
background image you want the text to appear on. }
TextImage.Height := BackgroundImage.Height;
TextImage.Width := BackgroundImage.Width;
{ In my limited experience with AlphaBlend, Black is always 100%
transparent. So, paint TextImage completely Black. Play around
with this to see the effect it has on the final outcome. }
TextImage.Canvas.Brush.Color := clBlack;
TextImage.Canvas.FloodFill(0, 0, clNone, fsBorder);
TextImage.Canvas.Font.Style := [fsBold];
{ Write the shadow first }
TextImage.Canvas.Brush.Style := bsClear;
TextImage.Canvas.Font.Color := clDkGray;
TextImage.Canvas.TextOut(11, 11, 'Test');
{ Then put the text on top (slightly offset) }
TextImage.Canvas.Brush.Style := bsClear;
TextImage.Canvas.Font.Color := clMaroon;
TextImage.Canvas.TextOut(10, 10, 'Test');
{ Use the AlphaBlend function to overlay the bitmap holding the text
on top of the bitmap holding the original image. }
Windows.AlphaBlend(BackgroundImage.Canvas.Handle, 0, 0,
TextImage.Width, TextImage.Height,
TextImage.Canvas.Handle, 0, 0, TextImage.Width,
TextImage.Height, BlendFunc);
{ Assign the now updated BackgroundImage to a TImage control for display }
Image1.Picture.Bitmap.Assign(BackgroundImage);
finally
TextImage.Free;
end;
finally
BackgroundImage.Free;
end;
end;
I presume what you're trying to accomplish is a little more complicated than simply writing text with a transparent background; i.e. you are trying to get some form of alpha-blended text written on the image.
The simplest method would be to make use of the GDI+ routines. They are encapsulated for delphi and available for download from http://www.progdigy.com/. There are many examples there which should be usable as an example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With