Writing Transparent Text on Image
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;
The shadow is easy:
// Bold shows up better when over an image
image1.Canvas.Font.Style := [fsBold];
// Write the shadow first
image1.Canvas.Brush.Style:=bsClear;
image1.Canvas.Font.Color := clGrayText;
image1.Canvas.TextOut(1, 1, 'hi there');
// Then put the text on top (slightly offset)
image1.Canvas.Brush.Style:=bsClear;
image1.Canvas.Font.Color :=clBlack;
image1.Canvas.TextOut(0, 0, 'hi there');
This is text with a transparent background. Or did you want the text itself to be simi-transparent? That is a little trickier. You would need to draw it manually. An easy way to do it instead would be to sample the average of the color of the area you are writing on the image. Then set your font color to be a little lighter and your shadow to be a little darker. Then it kind of blends in.
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.